|
Object Oriented PHP-GTK2 - OOP Basics Chapter 2
Object Oriented PHP-GTK2 - OOP Basics Chapter 2
|
||||
|
OOPS Site
Compile
OOP Basics Chapter 2The basics PHP5 object orientation, essential to successful PHP-GTK Topic: PHP5 Object Orientation (View All Tutorials)
Making Object Orientation Work For YouThere are three more features of PHP5 OOP that are important to making it truely useful for programming. Without them, OOP could be looked upon as extra lines of code. With them, a uniform system of application development.
Construction and Auto-DefaultsOne of the most useful features of classes is the ability to set default values on object creation. This is done by defining a method called __construct() in your class. This method works like other self-defined method except that when a new object is created with the new keyword it is automatically run. On the flip-side to that, the __destruct() method is run when the object is destroyed. For most general purposes it is not nessessary to define your own __destruct().
If you have ever burned a CD you have probably noticed that you can set the name of the disc, and you can only do it when you burn it. This is why the $title property is private, and I have created get and set methods for the title. This class also introduces a new item, the class constant defined with the keyword const. These set keywords just like the define() function, once set they cannot be changed. You can check the maximum amount of data an optical disc can have on it by reading that constant like so:
Max Data for Optical Disc: 650MB If you needed to access this class constant from inside the class, instead of referring to it as OpticalDisc::MAX_DATA you could read it as self::MAX_DATA - this way you could rename the class without having to edit the code inside. Class constants do not become passed to the objects, you always reference them statically. There is no need for them to be passed to the physical object because the constants job is to describe all objects of a type. So how about using this Optical Disc class?
Building Classes Off ClassesThe other great feature of object orientation is the ability to create a new class based off one that already exists. This prevents you from having to recreate the entire class. Before we extend our Optical Disc class though, we need to make one small change to it.
Notice the private property $title is now a protected property. I have done this because we want our new classes built off this one to be able to modify their inherited title property. Note protected properties allow new classes built off that class to use the property, but we still cannot use it from outside the classes, same as private. A Blank CD-R.
Notice this class also has a __construct() method, and it is built off the original Optical Disc class. Because we want CD-R's to inherit all of the attributes of an Optical Disc, the first line in the CD-R __construct() calls the parent constructor. The Parent? OpticalDisc is the Parent class. CDR is the Child class. The child inherits the Parent attributes and default values by executing the parent's constructor. The child then sets it's own default disc title.
When you extend a class, you also have the option to overwrite existing methods and class constants with new ones that pertain only to the child class (as well as any children classes built off that child class, thus it becomes a parent to the children as well) A Blank DVD-R.
Here we defined a new private property, $type, because Recordable DVD discs can be of a type plus or minus. When we set the type, it checks if the disc is still blank and updates the title with the proper type character. This class also overwrote OpticalDisc's MAX_DATA with it's own, since DVD's are quite a bit larger than normal CDs.
Overview Of Extended ClassesHere is an English translation of all the code above for our three classes, OpticalDisc, CDR, and SingleLayerDVDR.
A slightly more technical overview of the same three classes. This layout shows the parent-child relationship between the classes.
Some Common Questions asked, and some answers.
Static Properties and MethodsRefer back to the first instance of a class constant in this document. We had referenced it as "OpticalDisc::MAX_DATA". A breakdown of the physical syntax gives us "class::constant". This is known as referring to something that is a static member of the class. A member that is static can be accessed anywhere on the global scope where the class itself could be referenced, and it can be used independently of an object instance. In fact, an object never even has to be created to use static members, you could therefore use a class as a sort of Namespace for your data. Constants are always static, however both properties and methods can also be made static. Take this example class:
No matter where or when we read exampleClass::$var1 again, it will return 'orange'. And the static method, it will output the same thing every time. One note about static methods: unlike normal methods you do not get access to the $this object from within the method. This is because you have called the function statically which means without reference to an object. Also remember when working on the code inside of a class, you should reference static members of that class with the self:: prefix instead of the class name.
Wrapping UpRemember the rules about destroying objects. As your code becomes larger your system will have to devote more resources to it. This is true for any program. Free your objects when there is no use for them anymore.
Was this document helpful? I appreciate your feedback. i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ] |
||||