![]() |
PHP5 Classes A to Z Part 1
Due to some wanting a more advanced tutorial on classes and functions I'm back and ready to please, so lets get cracking shall we. For this tutorial we will be leaning more towards PHP5 so feel free to comment if something does not work for you. We will be doing an example of a mail class. Walking through it step-by-step ;-)
The improved object model in PHP5 makes developing applications using OOP much easier and gives you, the programmer greater flexibility. What is a class? A class is a collection of methods and objects. What is the purpose of a class? It's the same reason as any other programming language: for large projects, classes provide superior organization and less repetitive code. What is the difference between classes in PHP 4 & PHP 5? A few notable differences between the two versions are;
The class structure The class structure is pretty simple and straight forward; PHP Code:
Visibility of your data As from PHP5 visibility keywords have been added to the OOP model to make accessibility management easier for us. The visibility of a property or method can be defined by prefixing the declaration with the the visibility keywords;
As a side note although the keywords have been brought into the our PHP lives, this has not affected the var prefix, it is still acceptable to use it. It is equivalent to the public keyword. Lets see how our class would look with some visibility keywords; PHP Code:
Constructing and Destructing PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. Destructor is a function which is called right after you release an object. Releasing object means that you do not need it or use it anymore. This makes destructor suitable for any final actions you want to perform. PHP Code:
In the simplest sense, an abstract class is a class that cannot (and should not) be instantiated. This may sound a little illogical for beginning programmers, but there are situations where this concept makes a lot of sense. Abstract classes are very handy for large Web applications, where a well-structured hierarchy of classes is often required. Certainly, this doesn’t mean that you have to define abstract classes here and there, whenever you can possibly use them. Most of the time, implementing abstract classes requires careful planning and study of the relationships between the classes that make up an application. Abstract classes should only be used to be inherited or extended, for example; PHP Code:
An interface is similar to an abstract class, indeed the occupy the same namespace as classes and abstract classes, (hence you cannot define an interface with the same name as a class). An interface however, is a fully abstract class, none of its methods are implemented and, instead of a class subclassing from it, it is said to implement that interface. Each method declared in the interface must be defined by any class which implements it having at least the parameters identified. It may have more parameters (as long as they are optional), but it cannot have less. PHP Code:
Type Hinting is a new facility in PHP 5 which enables you to force function arguments to be objects of specific types. Before PHP 5, the only way of ensure that function arguments were of a specific object type was to use the type checking functions provided. Now we can simply enforce the object type by proceeding the function argument with its name. PHP Code:
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can). PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. PHP Code:
The Scope Resolution Operator or double colon :: allows us access to methods of a class. It is used in object-oriented programming when you want to be specific about what kind of function you are calling. The most common use for scope resolution is with the pseudo-class parent. Two keywords, self and parent are used to access members or methods inside a class. Outside the class; PHP Code:
PHP Code:
|
good stuff. I knew it all, but still good stuff.
|
Glad you enjoyed it
|
Good work dude! Your interface class example doesn't entirely make sense though, shouldn't TalkPHPMail have a receive() method defined? Your current example would surely fail?
|
Thanks pointing that out as i said it was 4 in the morning so the tiredness seemed to get the best of me, fixed the code it now includes the receive method. I'll publish part 2 of the guide which handles patterns and exceptions later this weekend when i come back from fishing :-D
|
Good stuff, Thanks :)
|
no problem ;-)
|
Tis Goodness.
|
Very good for beginners :-)
Good work! Notice: I think you missed the "public" when decalaring the functions in the first examples.. :-P |
Actually i used var instead of public because i was explaining the basic structure, you will see after that example we start with visibility keywords. That is where public came in :-P
|
Var is depreciated, It's fine for use as an example as above. But do know it probably wont be upwards compatible to PHP 6.
|
Yes it has been deprecated but it is still compatible with PHP5 which the guide was written for, and i only used it to explain the basic structure of the class the rest of the examples all use visibility keywords
|
| All times are GMT. The time now is 02:52 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0