LonghornPHP 2026

Voting

: six minus four?
(Example: nine)

The Note You're Voting On

Eugeny at Kostanay dot KZ
9 years ago
A snippet of code to help you understand a bit more about properties inside abstract classes:
<?php
abstract class anotherAbsClass
{
    // Define and set a static property
    static $stProp = 'qwerty';        // We can still use it directly by the static way
    // Define and set a protected property
    protected $prProp = 'walrus';
    // It is useless to set any other level of visibility for non-static variables of an abstract class.
    // We cannot access to a private property even inside a declared method of an abstract class because we cannot call that method in the object context.
    // Implementation of a common method
    protected function callMe() {
        echo 'On call: ' . $this->prProp . PHP_EOL;
    }
    // Declaration of some abstract methods
    abstract protected function abc($arg1, $arg2);
    abstract public function getJunk($arg1, $arg2, $arg3, $junkCollector = true);    
    // Note: we cannot omit an optional value without getting error if it has already been declared by an abstract class
}

class someChildClass extends anotherAbsClass
{    
    function __construct() {
        echo $this->callMe() . PHP_EOL;    // now we get the protected property $prProp inhereted from within the abstract class
    }
    // There must be implementation of the declared functions abc and getJunk below
    protected function abc($val1, $val) {
        // do something
    }
    function getJunk($val1, $val2, $val3, $b = false) {    // optional value is neccessary, because it has been declared above
        // do something
    }
}

echo anotherAbsClass::$stProp;    // qwerty
$objTest = new someChildClass;   // On call: walrus 
?>

<< Back to user notes page

To Top