Handling with Objects and Classes in PHP
Creating a class statement
You write the class statement to define the properties and methods for the class. The class statement has the following general format:
1
2
3
4
5
|
class class_Name
{
Put statements that define the properties here
Put all the methods here
}
|
You can use any sort of valid PHP identifier for the class name, except stdClass. PHP uses the name stdClass internally, so you can’t use this name.
All the property configurations and method definitions are surrounded in the opening and closing curly braces. If you need a class to be a subclass that inherits properties and methods, use a declaration similar to the following:
1
2
3
4
5
6
7
8
9
|
class blueRose extends Rose
{
put the property statements
put the methods here
}
|
The object developed from this class has access to all the properties and methods of both the blueRose child class and the Rose class. The Rose class, however, doesn’t have access to properties or methods in the child class, blueRose. Just imagine, the child owns everything the parent owns, but the parent owns nothing of the child’s. What an idea.
Using a Class in a Script
The class code needs to be in the script that uses the class. Most generally, the class is stored in a separate include file and is included in any script that uses the class.
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. Just as you can use a pattern to create many similar but individual dresses, you can use a class to create many equivalent but individual objects. To build an object, use statements that have the following format:
1
|
$object_name = new class_name(value,value,...);
|
Some valid statements that create objects are
1
2
3
4
|
$Rick = new Person(“male”);
$car_Rick = new Car(“red”);
$car_Sam = new Car(“green”);
$customer1 = new Customer(“Smith”,”Rick”,$custID);
|
The object is stored in the variable name, and the constructor method is executed. You can then use any method in the class with statements of the following format:
1
2
3
4
5
6
7
|
$Rick->goToWork();
$car_Rick->park(“illegal”);
$car_Sam->paintCar(“blue”);
$name = $customer1->getName();
|
Different objects created from the same class are independent individuals. Sam’s car gets painted blue, but Rick’s car is still red. Rick gets a parking ticket, but it does not have an effect on Sam.
The script shown in Listing below shows how to use the Form class
1
2
3
4
5
6
7
8
9
10
11
|
<?php
require_once(“Form.class”);
echo “<html><head><title>Phone form</title></head><body>”;
$phone_form = new Form(“process.php”,”Submit Phone”);
$phone_form->addField(“first_name”,”First Name”);
$phone_form->addField(“last_name”,”Last Name”);
$phone_form->addField(“phone”,”Phone”);
echo “<h3>Please fill out the following form:</h3>”;
$phone_form->displayForm();
echo “</body></html>”;
?>
|
First, the script includes the file containing the Form class in the script. The class is stored in the file Form.class. The script produces a new form object called $phone_form. Three fields are added with the addField method. The form is displayed with the displayForm method. Notice that some additional HTML code is result in this script. That HTML could have been added to the displayForm method just as easily. The set of scripts creates a form with three fields, using the Form class. Image below shows the resulting web page. If it’s shows blank page or different from image. you may have to check for common errors in programming.
Retrieve Information Objects and Classes
PHP provides numerous functions that you can use to obtain information about objects and classes:
1. You can confirm whether a class exists with the following:
1
|
class_exists(“class_name”);
|
2. You can test whether a property is present in a certain class with the following:
1
|
property_exists(“class_name”,”property_name”);
|
3. You can identify the properties, with their defaults, and the methods defined in a class with the following statements:
1
|
get_class_vars(“class_name”);
|
1
|
get_class_methods(“class_name”);
|
The get_class_ functions return an array. The properties array contains the property name as the key and the default as the value. The methods array contains numeric keys and the names of the methods as values. If a property or method is private, the function will not return its name unless it is executed from inside of the class.
4. You can check whether an object, its parents, or their implemented interfaces were created by a specified class using the instanceof operator, added in PHP 5, as follows:
1
|
if($objectname instanceof “class_name”)
|
5. You can find out the current values of the properties of an object with the following function:
1
|
get_object_vars($object_name);
|
The function returns an array that contain the actual values of the properties, with the property labels as keys.