My notes and thoughts about Linux, WordPress, PHP and many more.
You might read our introductory tutorial on how to learn PHP Object oriented programming (OOP).
Let’s start digging deeper into what are these classes, objects, methods and properties that are used in PHP OOP. Don’t be scared because this guide is written even an idiot can understand. It will be explained as easy as possible.
To understand these terms very clearly, get out of the world of programming and see the world around you. Everywhere you can see classes, objects, methods and properties. Where are these?
Everything you see in your eyes is objects. The house, cars, bicycles, your pet and the humans around you are objects. So this means that objects are entities. It is because even though you see humans everywhere, each of them is still unique and has specific characteristics.
Thus, human beings are called classes because it comprises different unique objects. The simplest way to understand classes is that they are group of objects.
Cars are also classes and these classes are different and unique car objects. For example Toyota, Ford and Ferrari are unique to each other despite belonging to same class (which is cars).
What makes an object under classes UNIQUE? It’s called “properties”. Properties are attributes that you can describe to a unique object. For example every single human being is object because of its unique properties. Human being can be described in terms of the following:
Above are called “properties” of single human object classified under “Human Being” classes.
But hey, didn’t these objects do something special and also unique? Every object has its own ability, capability and functionality. These are called the methods of the object. Let’s go back to the cars example. Since cars are classes with objects such as Toyota, Ford, BMW, etc. Each of these objects has similar functionality and features. The most basic of these features is that cars have the ability to start an engine. Thus this method is called “engine start”. There are several methods you can think of cars, for example- turning on the lights; switching on the air condition unit, switching on the car stereo and many more methods!
Other terms of methods are called “functions” because they do something when executed. Can you cite are the methods of a single human object? Yes humans can walk, run, swim, defecate, sleep, eat, laugh; fight; etc. But these methods are not available to cars classes. It is because classes, objects, properties and methods go together. One method of a specific object may not be available in another object.
Going back to the PHP object oriented programming world. The same definition and concept applies to classes, objects, methods and properties. In PHP, you define your own classes. And within this class are the objects, properties and methods unique to this class. If your PHP project demands a lot of features, you can create one class for each feature which you can place in separate PHP file called classes.php (example only, though you can name anything you want as long as it contains the word “class” or classes” for easy reference.) Remember in the first part that one of the benefits of object oriented programming is that it is modular, clean and well organized. Creating and defining class are the heart of OOP in PHP and it is the reason of its modular structure.
Using the summing and multiplying numbers example in the first part; you can organize these different classes in your PHP file such as:
Nice tutorial, basic thought for beginners.
Kindly continue this topic. Really helpful…
Thank you so much for your tutorial. I liked it! It’ll be cool if you tell us more about OOP in PHP! 🙂
I’m glad you found the tutorial helpful. Actually I’m on the process writing the tutorial series on PHP object oriented programming. It will be published anytime this month.Cheers.