Object Oriented Programming in PHP - Class 1: Principles
Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame
PHP began its OOP life with PHP4, and has a pretty decent object model with PHP5. The release of PHP6 - who knows when? - is destined to complete the object model nicely. The huge debate about whether OOP is better than procedural or not still rages on, with no end in sight. Whether this debate will ever be concluded is another story altogether, but I strongly believe that object oriented development is a better way to go. Perhaps this series of articles will show why.
In this class we will explore the essential principles of object oriented programming (OOP) with PHP. Our goal is to define what object oriented programming is, and what the advantages and disadvantages of using it are.
The Basics of Objects
In the real world we think of objects as real entities: a car, gate, or a light bulb. These entities can do things - cars can drive, gates can open or close and light bulbs can emit light. But there is more to this than meets the eye, because not only can objects do things, they have properties. A car can be travelling at a certain speed, and in a certain direction. A gate can be open, or closed, or busy closing or opening. A light bulb can be on or off, be emitting light at a certain temparature, be consuming electricity at a certain rate relative to its temparature and wattage.
PHP gives us a very simple way to define an object programmatically, and this is called a class. A class is a wrapper that defines and encapsulates the object along with all of its methods and properties. You can think of a class as a programmatic representation of an object, and it is the interface that PHP has given you, the developer, to interact with and modify the object. Moreover, a class may be re-used infinitely if need be, making it very powerful and codebase friendly. You do not have to redefine things every time you want to use an object - a properly coded class is setup to do all of the work for you.
Within the class wrapper, we can either define values of certain properties of the object, or tell the object to do things. Therefore it is important at this point to realise that a class has two main components: methods and properties. A method is essentially a function within a class, and a class may contain no methods, or thousands of methods. Properties are exactly what they seem to be - they are the properties of the object, and we usually use methods to set, modify and get properties from an object.
Our First Example
Throughout the course of these articles we shall be building an object that defines a very simple user object. This will be a very simple class defining a few things about users, as well as making some simple methods available to them.
In Figure 1 we see a very simple representation of the user object, defined by the class user. Let's look at it line for line:
- Line 3: We define the name of the object as "user".
- Line 5: We define the name of the user as a property. This will give us access to it throughout the class. The "public" part is the visibility accessor, and we'll get to that a little later. For now, all you need to know is that the "public" part pretty much means that the property "$name" is accessible from everywhere in the application.
- Line 7: We call the __construct() method. The constructor is a very special method that, when called, will run as the class in instantiated. This makes it a very powerful method that you can use to setup various things as the object is created, without having to call special methods. As the name suggests, it constructs the object. Also, we see that the variable "$name" has been passed into the constructor as an argument. This is possible because arguments can be passed into a class and then accessed via the constructor (more on this shortly).
- Line 9-11: We check if the name of the user is passed into the constructor as an argument, and, if not, we simply assign the name of the user as "John Doe". Note how this class is slightly flawed as it does not (yet) know the gender of the user, and assigns a male name by defualt. On line 10 we see the first use of the Pointer (->), as well as "$this". We'll get more into these as we proceed, but for now it is important to see that "$this" is the class refering to itself, and the pointer is pointing to the specific method or property. In this case, we are assigning a value to the property directly from the constructor class, which is also something that we should not really be doing. But for the purpose of this demonstration it will suffice.
Now, we may ask, how do we use all of this? Firstly, a class is instantiated (the object is created) by using the "new" operator, as in Figure 2, below.
As can be seen above, we have created a new object named "$user" and have not passed any arguments into the constructor. This means that the class will be instantiated and the property "$name" of the user object will have the value of "John Doe".
Figure 3 below shows us how to access the property directly:
The power of OOP
As we end this first part of the introduction to PHP OOP, we have briefly covered what an object is, how to create one using a class, and what methods and properties are. We have had a very brief look at how to access properties, and also what a constructor is and how it can be used to set up an object, We begin to see that power of PHP OOP, which lies in its reusability.
Our next class will deal with Methods And Properties in more detail, and after that we will be exploring the principles of OOP visibility.
What do you think of PHP Object Oriented Prgramming? Feel free to discuss this in the comments.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...