In my previous blog post, we looked at OOP and why you should be using it. If you missed it, you can get caught up by clicking here.
Now that you’re up to date we can look at creating our first object.
Object-oriented programming lets us group data as well as functions into a specific piece of functionality. In PHP, we call these groupings “classes”. Firstly, you need to understand Grouping. You can easily try to understand it by looking at objects in your day to day life. For example, your computer or mobile phone could be considered an object with storage, memory, and applications that are used to help you perform actions. If you practice grouping in your everyday life it will help perfect your understanding of what can make up an object. The next step is to look at the syntax.
Declaring a Class
When declaring a class the keyword “class” is used followed by the name and curly brackets. The standard is to start the curly brackets on the next line.
Naming a Class
Although a class is not case sensitive changing the naming conventions of your classes within a project can be confusing. Instead, you should follow the naming convention the standard being called “StudlyCaps”. This means the first letter should be capitalised, as well as the first letter of any new word with any other letter then being lowercase e.g. a class named “MyHouse”.
Instantiating an Object
After creating the class, a new object can now be instantiated using the keyword “New”.
$house1 = new House();
To add data into a class we can create variables inside the class. Any variables declared inside a class are called properties. Properties work like any other variables except they belong to the object and can only be accessed using the object. When declaring a property an access modifier must be added. Access modifiers allow us to control the access or visibility of properties.
- Public: Publicly accessible from anywhere, even from outside the scope of the class.
- Private: Accessed within the class itself. It protects properties and methods from being accessed from outside the class.
- Protected: Same as private, except allowing sub classes to access protected parent properties and methods.
When naming a properties the standard is called camelCase. The name starts with a lowercase letter and all other words start with a capital letter like the humps on a camel. For example $numberOfWindows.
public $numberOfWindows = 12;
Whenever we have an object we can start to access its properties. To get a data from an object we use the object name followed by the dash “-“ and the greater than “>” characters and finally the property name. The property name does not use the dollar sign “$”. Only the object name uses the $ sign because the entire object is its own variable. We can interact with this like any other variable. For example, to display a property value we can type echo before it. To set a property we use the equal sign and then the value.
public $numberOfWindows = 10;
$house1 = new House();
$house2 = new House();
$house2->numberOfWindows = 20;
When using multiple instances of the same class we can start to see the power of OOP. We’ve set up a house class to contain properties of a house. We’ve also created multiple instances of the house into individual objects that contain their own property values. Next, we’ll want to add some methods.
Adding a Method
A function inside a class is called a method. These methods tell an object how to perform certain actions. When declaring a method we must declare visibility just like we did with properties. The naming convention is also the same – camelCase.
public function showWindowCount()
return " Number of Windows " . $this->numberOfWindows;
$house1 = new House();
$house1->numberOfWindows = 10;
This is a simple method that displays the number of windows needed for a house. The method works like any other function, opening and closing parentheses, curly brackets that start on a new line to keep consistency with our class declaration. For a method to really work they need to access other properties or methods within the object. With PHP we can do this with the $this variable. In the sample we use $this to access the number of windows for our house object. To use the showWindowCount method we call it just like a normal function we just need to reference the object we want to use just like how we access a property.
This is a small look into the world of Object Oriented Programming. By learning about classes, properties, methods and objects you should be armed with the basic’s to start using OOP. Practice makes perfect, so start using OOP in your next project and get your hands dirty. Come back again when we’ll look at more complex classes by looking at access modifiers, array’s and cleaning up classes to make you a real master of OOP.