Basics of PHP Classes and Objects
Understanding Object Oriented Programming in PHP – Part 1
Foreword: In this part of the series I talk about the basics of PHP classes and objects.
By: Chrysanthus Date Published: 31 Jan 2014
Note: the output of all the code samples in this series are sent to the browser.
Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or just want to comment, contact me at email@example.com .
There are certain topics you need to have covered before you can understand this series. Click the link titled, “PHP Course” below to know what you should have studied before reaching here. This series is part of my volume, PHP Course. The volume consists of the Professional Course and the Advanced Course.
In the series titled, “Object Oriented Programming in PHP” of the professional course, I talked about OOP basics in general terms. There are certain topics I did not cover in that series. The content of that series will be summarized in this series. Details in OOP will be given and more new topics will be covered. The organization of the work in this series is also different.
A class is a set of variables and functions that work together. In this case the variables are called properties and the functions are called methods.
When values are given to the variables (properties) of a class, that class becomes an object. Since different sets of values can be given to the same class, a class can have many objects. An object is the instance of the class. Creating an object from the class is instantiating the class.
In this series, both properties and methods of the class will be called, members of the class or members of the object.
In the following code, a class is defined and instantiated. The class is called a Calculator. It adds two numbers.
public function add ()
$sum = $this->num1 + $this->num2;
$myObj = new Calculator();
$myObj->num1 = 2;
$myObj->num2 = 3;
$result = $myObj->add();
The class definition begins with,
The class has two properties, which are $num1 and $num2. Each of these properties will hold a value to be added together, in an instantiated object. The property declarations are preceded by the reserved word, public. The method to add the values of the two properties is called add(). In the method definition, the pseudo variable, $this, refers to the calling object, which in this case is the instantiated object. To use $this to access a property in the definition of the class, the syntax is:
Note that here, propertyVariable is not preceded by $.
To instantiate a class, you begin with the reserved word, “new” without the quotes, then a space and then the name of the class followed by parentheses. You instantiate a class outside the class description (definition). To access a property of the instantiated object (class), you use the syntax:
Note that here, propertyVariable is not preceded by $. To access a method of the object, you use the syntax:
You can create a class with default values. In this case the properties are initialized in the class definition. Any object instantiated from such a class has the values as object default values. Such values for the objects can still be changed. Read and try the following code:
public $prop1 = "man";
public $prop2 = "woman";
public function display()
echo $this->prop1 . " and " . $this->prop2;
$obj = new Cla();
$obj->display(); echo "<br>";
$obj->prop1 = "boy";
$obj->prop2 = "girl";
$obj->display(); echo "<br>";
Variable holding a Class Name
You can assign the class name as a string to a variable and use the variable when instantiating the class. The following code which you should read and try, illustrates this:
public $prop = "Yes";
$var = "Cla";
$obj = new $var();
That is it for this part of the series. We stop here and continue in the next part.
Related LinksBasics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
Date and Time in PHP
PHP String Functions for Website Design
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Web Application
More Related LinksMajor in Website Design
Web Development Course