Zend PHP 7 Certification – OOP – Instance Methods and Properties

This post covers the Instance Methods and Properties section of the OOP chapter when studying for the Zend PHP 7 Certification.

Class member variables are called properties. You may also see them referred to using other terms such as attributes or fields, but properties are the given name to describe variables belonging to a class.

<?php
class MyClass {
    public $someVar = "somevalue";
    protected $someInt = 4;
    private $someArray = array();
}

In order to maintain backward compatibility with PHP 4, PHP will still accept the use of the var keyword in property declarations.

<?php
class MyClass {
    var $someVar = "somevalue";
}

If you declare a property using var instead of one of public, protected, or private, then PHP will treat the property as if it had been declared as public.

The public visibility scope means that the property is available from anywhere, other classes and instances of the object.

<?php
class MyClass
{
    public $foo = 'public';
    
    function someMethod() {
        return $this->foo;
    }
}

$class = new MyClass();
echo $class->foo;           // Prints out 'public'
echo $class->someMethod();  // Prints out 'public'

The protected scope means that the property is visible in all classes that extend current class including the parent class.

<?php
class MyClass
{
    protected $foo = 'protected';
    
    function someMethod() {
        return $this->foo;
    }
}

$class = new MyClass();
echo $class->foo;           // Fatal error
echo $class->someMethod();  // Prints out 'protected'

The private scope means that the property is visible in its own class only.

<?php
class MyClass
{
    private $foo = 'private';
    
    function someMethod() {
        return $this->foo;
    }
}

$class = new MyClass();
echo $class->foo;           // Fatal error
echo $class->someMethod();  // Prints out 'private'

You can also declare multiple properties by using a comma as the delimiter.

<?php
class MyClass
{
    protected $a, $b;
    public $c, $d;
    private $e, $f;
}

As of PHP 5.3, heredocs and nowdocs can be used as part of the property declarations.

<?php
class foo {
   public $bar = <<<'EOT'
bar
EOT;
   public $baz = <<<EOT
baz
EOT;
}

As of PHP 5.6, you can define properties with simple expressions.

<?php
class MyClass
{
   public $var1 = 'hello ' . 'world';
   public $var2 = 1+2;
}

Within class methods non-static properties may be accessed by using the object operator, ->: $this->property (where property is the name of the property). Static properties are accessed by using the double colon, ::, along with the self keyword: self::$property.

<?php
class MyClass {
    public static $integer = 3;

    public function displayInteger() {
        return self::$integer;
    }
}

echo MyClass::$integer; // Outputs: 3

$myClass = new MyClass();
echo $myClass->displayInteger(); // Outputs: 3

Methods are functions within class constructs. Similar to properties, public, protected and private visibilities can be defined.

<?php
class MyClass
{
    // Declare a public constructor
    public function __construct() { }

    // Declare a public method
    public function MyPublic() { }

    // Declare a protected method
    protected function MyProtected() { }

    // Declare a private method
    private function MyPrivate() { }

    // This is public
    function Foo()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

$myclass = new MyClass();
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
class MyClass2 extends MyClass
{
    // This is public
    function Foo2()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate(); // Fatal Error
    }
}

$myclass2 = new MyClass2();
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private

You may find yourself writing code that refers to variables and functions in base classes. Instead of using the literal name of the base class in your code, you should be using the special name parent, which refers to the name of your base class as given in the extends declaration of your class.

<?php
class A {
    public function example() {
        echo "I am A::example() and provide basic functionality.
\n"; } } class B extends A { public function example() { echo "I am B::example() and provide additional functionality.
\n"; parent::example(); } } $b = new B(); // This will call B::example(), which will in turn call A::example(). $b->example();

View the other sections:

Note: This article is based on PHP version 7.0.