MemoryRefresh!

Oop / Resource visibility   Resource visibility



Questions 1 Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


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)
<?php 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.
<?php final class foo { final $c = 0; // Error: cannot declar property final // --- Look Here --- // 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).
<?php class foo { public $a = init(); // Syntax error // --- Look Here --- // public function init() { } } Static 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.
<?php error_reporting(E_ALL); class Foo { public static $to = "World"; public static function bar() { echo "Hello " . self::$to; } } echo foo::$to; // output: World $foo = new foo(); $foo->bar(); // output: Hello World echo $foo->to; // --- Look Here --- // // Notice: Undefined property: Foo::$to

Related Pages

Object instantiation
Class constructor
Resource visibility
Class constants
Interfaces, abstract
Namespaces


0% 100%
 
 
0 pages 91 pages
1) From where protected property can be accessed?





2) A resource can be accesed from the class only, if it is:








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)
<?php 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.
<?php final class foo { final $c = 0; // Error: cannot declar property final // --- Look Here --- // 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).
<?php class foo { public $a = init(); // Syntax error // --- Look Here --- // public function init() { } } Static 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.
<?php error_reporting(E_ALL); class Foo { public static $to = "World"; public static function bar() { echo "Hello " . self::$to; } } echo foo::$to; // output: World $foo = new foo(); $foo->bar(); // output: Hello World echo $foo->to; // --- Look Here --- // // Notice: Undefined property: Foo::$to

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References