The class is the template or pattern for the object. You write the class statement to define the properties and methods for the class. A class is defined by the keyword class followed by a valid class name and a block of code.
Add statements that define the properties
Add all the methods
The class body can contain properties and methods. All the property settings and method definitions are enclosed in the opening and closing curly braces. Properties are variables that hold the state of the object, whereas methods are functions that define what the object can do. In PHP, they need to have an explicit access level specified.
You can use any valid PHP identifier for the class name, except the name stdClass. PHP uses the name stdClass internally, so you can’t use this name.
If you want a class to be a subclass that inherits properties and methods, use a statement similar to the following:
class whiteRose extends Rose
Add the property statements
Add the methods
The object created from this class has access to all the properties and methods of both the whiteRose child class and the Rose class. The Rose class, however, doesn’t have access to properties or methods in the child class, whiteRose.
When you are defining a class, you declare all the properties at the top of the class. Properties are like variables inside the class. You can use these variables in objects.
Each property declaration begins with a keyword that specifies how the property can be accessed. The three keywords are:
public: The property can be accessed from outside the class, either by the script or from another class.
private: No access is granted from outside the class, either by the script or from another class.
protected: No access is granted from outside the class except from a class that’s a child of the class with the protected property or method.
Classes should be written so that methods are used to access properties. By declaring a property to be private, you make sure that the property can’t be accessed directly from the script.
To set or change a variable’s value when you create an object, use the constructor.
Inside a class, $this is a special variable that refers to the properties of the same class. The $this can't be used outside of a class. It is designed to be used in statements inside a class to access variables inside the same class.
A dollar sign ($) appears before this but not before variable name.
Methods are functions defined inside a class. Like functions, methods get some arguments and perform some actions, optionally returning a value. The advantage of methods is that they can use the properties of the object that invoked them. Thus, calling the same method in two different objects might have two different results.
Methods define what an object can do and are written in the class in the same format you’d use to write a function.
private $total = 0;
$this->total = $this->total + $amount;
echo “$amount was added; current total is $this->total”;
Like functions, methods accept values passed to them.
The $this variable represents the object itself, and allows you to access the properties and methods of that same object.
Methods can be declared public, private, or protected, just as properties can. Public is the default access method if no keyword is specified.
PHP provides some special methods with names that begin with __ (two underscores). PHP handles these methods differently internally.
How to Use Class in a Script
The class code needs to be in the script that uses the class. Most commonly, the class is stored in a separate file and is included in any script that uses the class.
Instantiating an Object
To use an object, you first create the object from the class. Then, that object can perform any methods that the class includes. Creating an object is called instantiating the object. You can use a class to create many similar but individual objects. To create an object, use statements that have the following format:
$objectname = new classname(value,value,...);
The object is stored in the variable name.
Accessing Object Members
To access members that belong to an object, the single arrow operator (->) is needed. It can be used to call methods or to assign values to properties.
Conventions for Classes
When working with classes, you should know that there are some conventions that everyone tries to follow in order to ensure clean code which is easy to maintain. The most important ones are as follows:
Each class should be in a file named the same as the class along with the .php extension.
Class names should be in CamelCase, that is, each word should start with an uppercase letter, followed by the rest of the word in lowercase.
A file should contain only the code of one class.
Inside a class, you should first place the properties, then the constructor, and finally, the rest of the methods.