PHP & OOP: Instance of a class

This entry is part 4 of 11 in the series PHP & OOP

The keyword new is to create an instance of a class.

class SimpleClass
    public $var = 'a default value';

    public function displayVar() {
        echo $this->var;

//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayVar();


As in the line

$simple_object = new SimpleClass();

You can also think of it as creating an object from the class SimpleClass.  With the instance $simple_object, we can access the class variable using


In PHP, we have used -> operator.  In other programming languages such as C++, we would have used the dot(.) operator.

Series Navigation<< PHP & OOP: A simple class declaration
PHP & OOP: $this variable >>