We have already discussed what are classes and objects in Object-Oriented Programming. Now we will see how to create PHP classes and their objects.
In order to create a Class in PHP, you have to follow some naming conventions. These are not mandatory, but to write well-organized code using OOP, you have to implement the following conventions.
PHP class naming conventions
- Class names should always start with a capital letter. If a class name contains more than two words, use PascalCase. Example – fruit =>
Fruit
, search user =>SearchUser
. - The class name and the file name should be the same. For example, if a class name is
Apple
, then the file name should beApple.php
.
Create a PHP class
Using the class
keyword you can create a Class in PHP.
You have to write the class keyword before the class name (The class name and class keyword are separated by a space.), and after the class name, add the curly braces {}
where you will define the methods and properties of the class.
<?php
class ClassName{
// Methods and Properties
}
Example of a PHP class
In the following code you can see that there is a PHP class named MyClass
.
The class has a property called $name
whose value is John
, and there is a method called sayHello
.
Now you can see that there is a public
keyword before the property and the method. This is an access modifier. There are 3 types of access modifiers in PHP – public
, private
, and protected
.
It is mandatory to add an access modifier before the property name. But If you not specify an access modifier before a method, then the default modifier (public
) will be assumed for the method.
<?php
class MyClass{
// This is a Property (Variable)
public $name = 'John';
// This is a Method (Function)
public function sayHello(){
echo 'Hello';
}
}
PHP class Instance or Object
As we all know that, to access or use the properties and methods of a class you have to create an instance or object of the class.
So, using the new
keyword you can create an instance or object of a class in PHP.
new ClassName();
// assigning an object to a variable
$var1 = new ClassName();
Object Operator ->
To access the properties and methods of an object you have to use the object operator (->
).
<?php
class MyClass{
// This is a Property (Variable)
public $name = 'John';
// This is a Method (Function)
public function sayHello(){
echo 'Hello';
}
}
$object1 = new MyClass();
// Don't add $ sign before the property name
echo $object1->name;
// For new line
echo "\n";
$object1->sayHello();
John
Hello
Multiple instances of a class in PHP
You can create multiple instances of a PHP class, and they are completely different from each other.
The following MyClass has a property called $name
whose default value is John
, and there is a showName
method that displays the name.
You can also see that there are two instances of MyClass: $object1
and $object2
and they are absolutely different.
To understand that you can see we have changed the value of the $object1->name
property to 'Rocky'
, but the $object2->name
is still 'John'
, because they are different.
<?php
class MyClass{
public $name = 'John';
public function showName(){
return $this->name."\n";
}
}
$object1 = new MyClass();
$object2 = new MyClass();
$object1->name = 'Rocky';
echo "Object 1 - ".$object1->showName();
echo "Object 2 - ".$object2->showName();
Object 1 - Rocky
Object 2 - John