Memory App


A resource can be: - public (accesed from any scope) - protected (from within the class and its descendants) - private (from within the class only) - final (from any scope, but cannot be overriden in descendants)

    class foo {
        public $bar;
        protected $baz;
        private $bas;

        public $var1 = "Test"; // String
        public $var2 = 1.23; // Numeric value
        public $var3 = array (1, 2, 3);


Final visibility applies to methods and classes only.

    final class foo {
        final $c = 0; // Error: cannot declar property final

        final public function sum($a, $b) {
            return $a + $b;


Constructors (and all other magic methods) are normaly declared public (but constructors are private in patterns like Singleton). You can't initialize a variable by calling a function (that is done in constructor).

    class foo {
        public $a = init(); // Syntax error
        public function init() {}


Static methods or properties can be access only as part of a class itself. This allows true encapsulation and avoid naming conflicts. Accessing static with -> generates notice.

        class Foo {
            public static $to = "World";
            public static function bar() {
                echo "Hello " . self::$to;

        echo foo::$to;
            // output: World
        $foo = new foo();
            // output: Hello World
        echo $foo->to;
            // Notice: Undefined property: Foo::$to


Top Reference > Programming
0/0 (80)  
Not Logged