Studyon Minte9.com
ZCE 5.3

Study

Methods & Properties



	// Constructors - usefull for initializing an object's properties
	// Destructors - unset($obj) doesn't destroy object references
	// Protected - from within the class or its descendants
	// Constants - class constants are public, and accesible from all scopes
	// Static - $foo->a; // Notice: Undefined property: Foo::$a
        // Refelexion; Get private properties; Get parent private properties

Contructors

The concept of contructor was already used in PHP 4 (add a method with the name of the class).
If you renamed the class you would also have to rename the constructor. Php 5 uses the magic 
__construct() method as constructor. Constructor is usefull for initializing object's properties.

<?php // If the __construct() method is not found, // PHP will look for the old PHP 4-style constructor (foo) class foo { // PHP 5 function __construct() { // --- Look Here --- // echo __METHOD__; } // PHP 4 function foo() { echo __METHOD__; } } $foo = new foo(); // output foo::__construct $foo->foo(); // output foo::foo
  Destructors When you use unset() or overwrite a variable that is reference for an object, the object itself may not be destroyed. The reference to it is held elsewhere.
<?php class A { function __construct() { } function foo() { echo "A is alive"; } } $a = new A(); echo $a->foo(); // A is alive $b = $a; unset($a); // --- Look Here --- // echo $b->foo(); // A is alive // --- Look Here --- // echo $a->foo(); // Fatal error: Call to a member function foo() on a non-object
  The __destruct() method is called at the end of script execution.
<?php class A { function __construct() { } function foo() { echo "A is alive"; } function __destruct() { echo "A is dead"; } } $a = new A(); echo $a->foo(); // A is alive $b = $a; unset($a); echo $b->foo(); // A is alive // A is dead // --- Look Here --- //
  Visibility 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
  Class Constants Class constants are public, and accessible from all scopes.
<?php class foo { const BAR = "Hello World"; } echo foo::BAR; // output Hello World // --- Look Here --- //
  Class constants have the same limitations as normal constants.
<?php class foo { const ARR = array(); // Arrays are not allowed in class constants // --- Look Here --- // }
  Get private property
<?php class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } $foo = new Foo(); $reflector = new ReflectionObject($foo); //$private = $reflect->getProperties(ReflectionProperty::IS_PRIVATE); $property = $reflector->getProperty('baz'); $property->setAccessible(true); echo $property->getValue($foo); // display 3
  Get private properties
<?php class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } $foo = new Foo(); $reflector = new ReflectionObject($foo); $properties = $reflector->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); array_map(function(&$x) { $x->setAccessible(true); }, $properties); var_dump($properties); /* array (size=2) 0 => object(ReflectionProperty)[3] public 'name' => string 'bar' (length=3) public 'class' => string 'Foo' (length=3) 1 => object(ReflectionProperty)[4] public 'name' => string 'baz' (length=3) public 'class' => string 'Foo' (length=3) */
  Get parent properties
<?php class Ford { private $model; protected $foo; public $bar; } class Car extends Ford { private $year; } $class = new ReflectionClass('Car'); var_dump($class->getProperties()); // First chunk of output var_dump($class->getParentClass()->getProperties()); // Second chunk
 


http://php.net/manual/ro/reflectionclass.getproperties.php
http://stackoverflow.com/questions/9913680/does-reflectionclassgetproperties-also-get-properties-of-the-parent