Broad Network


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

Introduction

This is part 1 of my series, Understanding Object Oriented Programming in PHP. In this part of the series I talk about the basics of PHP classes and objects. OOP stands for Object Oriented Programming. This is a tutorial.

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 forchatrans@yahoo.com .

Pre-Knowledge
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
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.

An Object
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.

Defining and Instantiating a Class
In the following code, a class is defined and instantiated. The class is called a Calculator. It adds two numbers.

<?php

    class Calculator
        {
            public $num1;
            public $num2;
    
            public function add ()
                {
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

    $myObj = new Calculator();

    $myObj->num1 = 2;
    $myObj->num2 = 3;

    $result = $myObj->add();
    echo $result;

?>

The class definition begins with,

    class Calculator

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:

    $this->propertyVariable

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:

    $objectName->propertyVariable

Note that here, propertyVariable is not preceded by $. To access a method of the object, you use the syntax:

    $objectName->methodName()

Default Values
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:

<?php

    class Cla
        {
            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:

<?php

    class Cla
        {
            public $prop = "Yes";
        }

    $var = "Cla";
    $obj = new $var();
    echo $obj->prop;

?>

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Basics 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
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
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 Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message