How to build a class object in one line

Object oriented programming (oop) is not something that is new, as a matter of fact, it has been around for many many years. Almost all of your major programmers use oop to build just about everything, as well as all your major programming languages have the capability of oop.

So many years into programming, I decided to find a new way to create my objects with an on the fly type of implementation as compared to the normal implementation of multiple lines in your code. To be honest, I got tired of typing all the declarations over and over and over again.

There are many ways to implement object creation, however, I find the most simple solutions to be the fastest, such as one line compared to two lines for each object. Since my web-sites are written in php, I’m using php as my example here. (and yes this is working code, I use it)

The most common way to create an object in php is to include the class file and then create the object. This is performed by the following code.
require_once('class_filename.php');
$object = new class_name();

Now after many years of doing the above creation method, you’ll notice a patern, which is to say the least, ugly, as you will have many lines of the require_once and then another line for the creation. Now I know you can have a one file method, which will create all your objects in your file, and then you just include that file in every file. Even more useless, as now you may be building objects in files that do not need that object. (confused? me too)

However, I decided, since my code will always use a session class. Basically this is a class that manipulates the session variables in the web server memory (in very very short terms) I decided to find a new way to implement object creations. And this is how I did it.

The goal of this test or new method, is to find the fastest way to create an object will as little code as computerly possible, and well… I did in it in 12 lines, this also includes the created of the object.

The implementation of the code is very simple, left-hand assigned a variable name the return of a function that is passed a file name and the class name. The code looks like this:
$debug = $session->loadThisClass('library/debug_class.php', 'debug_class');
As you can see, I am assigning the variable name debug as the object, which is returned from the function loadThisClass(). Now when I want to perform a function or access a variable from this class, I would use the normal object access method
$debug->display($debug,'debug_class');
Which you can probably determine what is going on with that function display().

Now that the implementation is out of the way, we must define the function and execution of this function. $debug = $session->loadThisClass('library/debug_class.php', 'debug_class');
Located in my session_class file, I created a public function that performs the magic. Now you could implement this in any class file you have, However I do recommend that you include it in a class file that you would be already including in every file. Such as a session class, or a server class file. So here’s the code.


	public function loadThisClass($filename = null, $className){
		if($filename != null && $filename != ''){
			if(file_exists($filename)){
				require_once($filename);
				return new $className();
			}
		}
		return false;
	}

The above code, is in my opinion, simple and short, however, this code was written and not fine tuned, so I am aware that this code could have some fine tuning done, and cleaned up even more...and I probably will do that. But let's break down the parts.
public function loadThisClass($filename = null, $className){
We declare the class function, and statically declare the passed in variables.

if($filename != null && $filename != ''){
We verify that the file name is not empty and not null. Function can not include '' as a filename.

if(file_exists($filename)){
Check to ensure the file does exists

require_once($filename);
Include the file name

return new $className();
Now this is the real magic, as I spent many hours of trying to figure this one out, as it WOULD NOT work any other way that I tried, and I tried a lot. Basically this returns the creation of the object. This is so the object is passed back to your left-hand assignment from your code of implementations.

In conclusion, you can code a little faster, and have 1 less line in your file for object creatation. However I am not saying your code may or may not still be ugly, but in your index file you will not have two lines of code to create an object, instead you will have one. Here's a snippet of one of my index files. (I use vim)


1 require_once('library/session_class.php');
2 $session = new session_class();
3
4 $debug = $session->loadThisClass('library/debug_class.php', 'debug_class');
5 $clients = $session->loadThisClass('library/client_class.php', 'client_class');
6 $domains = $session->loadThisClass('library/domain_class.php', 'domain_class');
7 $incidents = $session->loadThisClass('library/incident_class.php', 'incident_class');
8

As you can see, this looks a lot better then


1  require_once('library/session_class.php');
2  $session = new session_class();
3  require_once('library/debug_class.php');
4  $debug= new debug_class();
5  require_once('library/client_class.php');
6  $client = new client_class();
7  require_once('library/domain_class.php');
8  $domains = new domain_class();
9  require_once('library/incident_class.php');
10 $incidents = new incident_class();

You can clearly see the difference, now of course the second way can be massive, if you have a lot of class files, which I do, however, I only load one session, and then create objects from that session object as I need them. Happy coding.

Ubuntu 10.04 and KVM – no GUI

So I have being attempting to bridge my network interface from my guest OS to my host network. And in short, here is the howto. I’m not using a GUI, however if you have a desktop… apt-get install virt-manager … and save yourself some MAJOR headaches. Hope this helps others… which I could have some like this to begin with.

NOTE :: These are quick notes, and walk through, of 8 hours of fiddling with a test machine to get it working, and then again on a production machine to get it working.

1. Install Ubuntu 10.04 (x86 or amd64) LTS Server edition
a. ensure you choose ‘Virtual Host Server’ as a service to run.
2. reboot from installation
3. aptitude update && aptitude -y safe-upgrade
4. reboot again
5. sudo apt-get install libcap2-bin virtinst
6. virt-install –prompt
a. I modify the XML file to include VNC so I can VNC remote into the machine.
7. Setup your VM.
—[ NOW THE FUN ]—
The next steps took my an entire day to perform these task. Everything I had read started to go back into the desktop, well we are running a stripped down server, with NO desktop.

1. Create the bridge interface file (copy and paste – modify to suit your needs)
vim /etc/libvirt/qemu/networks/bridge.xml
<network>
<name>bridge</name>
<!–<uuid>some_uuid_for_this_network_device</uuid>–>
<bridge name=’br1′ stp=’off’ delay=’0′/>
</network>
SAVE and QUIT
2. Create the network interface for use with the KVM or QEMU
virsh net-define /etc/libvirt/qemu/networks/bridge.xml
virsh net-create /etc/libvirt/qemu/networks/bridge.xml

3. Modify /etc/network/ointerfaces – add these lines to enable the Bridge to operate on a LAN (either by way of DHCP or static IP
# interface name must match the bridge name – DHCP passthrough
auto br1
iface auto br1 dhcp
metric 100
bridge_ports eth1
bridge_hello 2
bridge_maxage 12
bridge_stp off

# interface name must match the bridge name – STATIC passthrough
auto br1
iface auto br1
address 101.102.103.104
network 101.102.103.0
netmask 255.255.255.0
broadcast 101.102.103.255
gateway 101.102.103.1

SAVE and QUIT

4. Start your VM and verify.
virsh create /etc/libvirt/qemu/virtual_machine.xml

—This is dirty and should be taken with a grain of patience…. ALOT OF PATIENCE
References
http://ubuntuforums.org/showthread.php?t=1670656
http://www.ideyatech.com/2010/05/virtualization-with-ubuntu-1004-lucid-lynx/
http://www.systemajik.com/blog/manual-networking-for-kvm/
http://unix.stackexchange.com/questions/8673/bridged-networking-with-kvm
https://help.ubuntu.com/community/KVM/Networking#bridgednetworking
http://ubuntuforums.org/showthread.php?t=966739
https://help.ubuntu.com/community/KVM/Directly