PHP & OOP: A simple class declaration

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

We first look at this simple class declaration.

class SimpleClass
    // property declaration
    public $var = 'a default value';

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

$simple_object = new SimpleClass();
echo $simple_object->displayVar();


In object-oriented terminology, a property is actually the variable of a class and a method is a function declared inside the class.

In the above example, we have used the method displayVar() to access the variable $var.  The variable $var is a property of the class SimpleClass.


The visibility or scope of a property or method can be defined by prefixing the declaration with the keywords public, protected or private.

Class members declared public can be accessed everywhere.

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

Members declared as private may only be accessed by the class that defines the member.

Series Navigation<< PHP & OOP: The OOP core ideas
PHP & OOP: Instance of a class >>

Leave a comment

Leave a Reply