Class
A class is a generalized unit from which objects can be instantiated (created). A class is basically a code unit that has variables and functions that work together. The variables are called properties and the functions are called methods. A class itself cannot solve a problem; that is, a class itself cannot carry out a task. It is an object created from the class that carries out a task; not the class.

When you create an object from a class, we say you are instantiating the object. Properties and methods of the class are called members of the class. An object created from a class has the same members as the class.

A Class and Object created from the above Code
The above code can be converted into a class and object as follows:

<?php

    class Calculator
        {
            public $num1;
            public $num2;

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

        $myObject = new Calculator();
        $myObject->num1 = 2;
        $myObject->num2 = 3;
        $result = $myObject->add();
        echo $result;

?>

You define a class beginning with the reserved word, class. Then you have a space and then follow it with the name of the class. You choose whatever name you want for the class. I have given the name Calculator because the class is doing some calculation. After the class name, you have a pair of curly brackets. There are statements and even blocks inside the curly brackets. All the statements for the class go inside the braces. It is conventional to type the variables first before the functions. The variables in the class are called properties (of the class) and the functions in the class are called methods. Both properties and methods are called members. In the creation of a basic class as the ones we are considering in this tutorial, you precede the statements in the class with the reserved word, public. The variables and the function we had in the previous code are the same variables and functions we now have in the class.

The reserved variable, $this refers to the object of the class. Note how it has been used to access the members of the class (object). In the class description, you use $this followed by -> and then the name of the member without any preceding $.

Read through the creation of the class above to appreciate how a class is created (described).

Under normal circumstances, you define the properties (variables) in a class, and you do not initialize them (give them initial values). That is why in the above class, num1 and num2 do not have any values assigned to them. There is what is called constructor function that can be used to assign initial values to them, when an object is created from the class (see below).

Creating an Object from the Class without Constructor Function
A class like the one above does not have a constructor function (see below). When a class does not have a constructor function, you create (instantiate) an object from it using the reserved word, new, such as in the line,

        $myObject = new Calculator();

where $myObject is the name of your choice for an object, and Calculator() is like a function call to the class to create the object. This is done outside the class description (definition).

Using an Object
The aim of the class and object is to solve the problem, which the first code above solved. It is to add two numbers that are in two variables. You cannot use a class, you use but objects created from the class. Members of a class automatically become members of the instantiated object. You can create many objects from a class; the main thing you need is different variable names for the objects. To access a member of an object, you begin with the name of the object. This is followed by -> (minus sign then greater than sign), and then the name of the member without any preceding $. If the member is a method (function), you will have to follow the name with parentheses. These parentheses may have arguments, if the declaration or definition of the function had parameters.

To solve the above problem, we need to assign values to the properties (num1 and num2). This is what the second and third statements below the class description do. An object will not just solve your problem by itself. An object normally has one or more methods that you call to accomplish a particular task, using one or more properties of the object. The add method (function) of our object, does the addition using the two properties of the object; because of the way we defined the method. The fourth statement below the class description calls the add method and assigns the return value to a new variable, $result. The fifth statement displays the result.

The Constructor Function
If you want to create an object and at the same time initialize (assign values to) the properties, then you need what is known as the constructor function while typing the class. After this, to instantiate (create) an object from the class, you have to place the initial arguments in the class call. The following code illustrates this.

<?php

    class Calculator
        {
            public $num1;
            public $num2;

            function __construct($no1, $no2)
                {
                    $this->num1 = $no1;
                    $this->num2 = $no2;
                }

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

        $myObject = new Calculator(2,3);
        $result = $myObject->add();
        echo $result;

?>

Note how the constructor function has been defined using the reserved word, __construct and parameters. Outside (below) the class description the values were not assigned to the object again.

Default Values
To have default values for the properties, just initialize the properties in the aclass. The following code illustrates this:

<?php

    class Calculator
        {
            public $num1 = 2;   #default value
            public $num2 = 3;   #default value

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

        $myObject = new Calculator();
        $result = $myObject->add();
        echo $result;

?>

The constructor (function) is not really needed here. Outside (below) the class description the values were not assigned to the object again.

There is more to Classes than I have given. However, I will not go into those extra bits in this basic tutorial series. Let us stop here and continue in the next part.

Chrys

Related Links

Basics of PHP with Security Considerations
More Related Links
Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
Cousins
JavaScript/ECMAScript Course
Perl Course

BACK NEXT

Comments