diff --git a/language/oop5/abstract.xml b/language/oop5/abstract.xml index 4cc8d823e9..3122eff03e 100644 --- a/language/oop5/abstract.xml +++ b/language/oop5/abstract.xml @@ -1,5 +1,5 @@ - + Object Abstraction @@ -23,32 +23,29 @@ getValue(); - } - + // Common method + public function printOut() { + print $this->getValue(); + } } -class ConcreteClass1 extends AbstractClass { - - protected function getValue() { - return "ConcreteClass1"; - } - +class ConcreteClass1 extends AbstractClass +{ + protected function getValue() { + return "ConcreteClass1"; + } } -class ConcreteClass2 extends AbstractClass { - - protected function getValue() { - return "ConcreteClass2"; - } +class ConcreteClass2 extends AbstractClass +{ + protected function getValue() { + return "ConcreteClass2"; + } } diff --git a/language/oop5/autoload.xml b/language/oop5/autoload.xml index 4d22e00026..8d9848d67c 100644 --- a/language/oop5/autoload.xml +++ b/language/oop5/autoload.xml @@ -1,5 +1,5 @@ - + Autoloading Objects @@ -26,8 +26,7 @@ - + The Basics @@ -21,14 +21,15 @@ var; /* Echo my own $var value */ - } + // method declaration + public function displayVar() { + echo $this->var; + } } ?> ]]> @@ -70,11 +71,9 @@ $instance = new SimpleClass() $assigned = $instance; $reference =& $instance; - $instance->var = '$assigned will have this value'; -$instance = null; /* $instance and $reference become null */ - +$instance = null; // $instance and $reference become null var_dump($instance); var_dump($reference); @@ -116,13 +115,14 @@ object(SimpleClass)#1 (1) { - + Object cloning @@ -30,13 +30,11 @@ $copy_of_object = clone $object; - When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references. If a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed. - @@ -44,31 +42,31 @@ $copy_of_object = clone $object; instance = ++self::$instances; + } - public function __construct() { - $this->instance = ++self::$instances; - } - - public function __clone() { - $this->instance = ++self::$instances; - } + public function __clone() { + $this->instance = ++self::$instances; + } } -class MyCloneable { +class MyCloneable +{ + public $object1; + public $object2; - public $object1; - public $object2; - - function __clone() { - - // Force a copy of this->object, otherwise - // it will point to same object. - $this->object1 = clone($this->object1); - } + function __clone() + { + // Force a copy of this->object, otherwise + // it will point to same object. + $this->object1 = clone($this->object1); + } } $obj = new MyCloneable(); diff --git a/language/oop5/constants.xml b/language/oop5/constants.xml index e468543394..6cfdbfb177 100644 --- a/language/oop5/constants.xml +++ b/language/oop5/constants.xml @@ -1,5 +1,5 @@ - + Object Constants @@ -15,19 +15,20 @@ showConstant(); -/* echo $class::constant; is not allowed */ +// echo $class::constant; is not allowed ?> ]]> diff --git a/language/oop5/exceptions.xml b/language/oop5/exceptions.xml index 55922f0f5e..9699363c24 100644 --- a/language/oop5/exceptions.xml +++ b/language/oop5/exceptions.xml @@ -1,5 +1,5 @@ - + Exceptions @@ -50,7 +50,8 @@ try { - + Object Iteration @@ -15,26 +15,24 @@ $value) { - print "$key => $value\n"; + print "$key => $value\n"; } ]]> - - Will output: - + &example.outputs; value 1 @@ -59,52 +57,52 @@ var3 => value 3 var = $array; + public function __construct($array) + { + if (is_array($array)) { + $this->var = $array; + } } - } - public function rewind() { - echo "rewinding\n"; - reset($this->var); - } + public function rewind() { + echo "rewinding\n"; + reset($this->var); + } - public function current() { - $var = current($this->var); - echo "current: $var\n"; - return $var; - } + public function current() { + $var = current($this->var); + echo "current: $var\n"; + return $var; + } - public function key() { - $var = key($this->var); - echo "key: $var\n"; - return $var; - } + public function key() { + $var = key($this->var); + echo "key: $var\n"; + return $var; + } - public function next() { - $var = next($this->var); - echo "next: $var\n"; - return $var; - } - - public function valid() { - $var = $this->current() !== false; - echo "valid: {$var}\n"; - return $var; - } + public function next() { + $var = next($this->var); + echo "next: $var\n"; + return $var; + } + public function valid() { + $var = $this->current() !== false; + echo "valid: {$var}\n"; + return $var; + } } $values = array(1,2,3); $it = new MyIterator($values); foreach ($it as $a => $b) { - print "$a: $b\n"; + print "$a: $b\n"; } ]]> @@ -150,19 +148,19 @@ valid: items); - } - - public function add($value) { - $this->items[$this->count++] = $value; - } + // Required definition of interface IteratorAggregate + public function getIterator() { + return new MyIterator($this->items); + } + public function add($value) { + $this->items[$this->count++] = $value; + } } $coll = new MyCollection(); @@ -171,7 +169,7 @@ $coll->add('value 2'); $coll->add('value 3'); foreach ($coll as $key => $val) { - echo "key/value: [$key -> $val]\n\n"; + echo "key/value: [$key -> $val]\n\n"; } ?> diff --git a/language/oop5/magic.xml b/language/oop5/magic.xml index e5a9ea35a6..996e4b9de1 100644 --- a/language/oop5/magic.xml +++ b/language/oop5/magic.xml @@ -1,9 +1,11 @@ - + Magic Methods The function names + __construct, + __destruct, __sleep, __wakeup, and __toString @@ -61,7 +63,8 @@ - + Comparing objects @@ -25,34 +25,38 @@ flag = $flag; + function Flag($flag = true) { + $this->flag = $flag; } } -class OtherFlag { - var $flag; +class OtherFlag +{ + public $flag; - function OtherFlag($flag=true) { - $this->flag = $flag; + function OtherFlag($flag = true) { + $this->flag = $flag; } } diff --git a/language/oop5/overloading.xml b/language/oop5/overloading.xml index daa9608bc4..a18b29c727 100644 --- a/language/oop5/overloading.xml +++ b/language/oop5/overloading.xml @@ -1,5 +1,5 @@ - + Overloading @@ -36,11 +36,13 @@ 1, "b" => 2, "c" => 3); - function __get($nm) { + function __get($nm) + { print "Getting [$nm]\n"; if (isset($this->x[$nm])) { @@ -52,7 +54,8 @@ class Setter { } } - function __set($nm, $val) { + function __set($nm, $val) + { print "Setting [$nm] to $val\n"; if (isset($this->x[$nm])) { @@ -131,10 +134,12 @@ object(Setter)#1 (2) { x; diff --git a/language/oop5/paamayim-nekudotayim.xml b/language/oop5/paamayim-nekudotayim.xml index 27915e06b7..c1004602a0 100644 --- a/language/oop5/paamayim-nekudotayim.xml +++ b/language/oop5/paamayim-nekudotayim.xml @@ -1,5 +1,5 @@ - + Scope Resolution Operator (::) @@ -29,8 +29,9 @@ ]]> @@ -47,13 +48,14 @@ echo MyClass::CONST_VALUE; - + Reflection @@ -18,16 +18,16 @@ ]]> @@ -44,12 +44,12 @@ ]]> - We will see: + &example.outputs; class Exception ] { @@ -116,22 +116,23 @@ Class [ class Exception ] { ]]> @@ -154,7 +155,6 @@ Class [ class Exception ] { function counter() { static $c = 0; - return $c++; } @@ -213,16 +213,17 @@ echo ReflectionFunction::export('counter'); ]]> @@ -243,34 +244,33 @@ echo ReflectionFunction::export('counter'); getParameters() as $i => $param) - { - printf( - "-- Parameter #%d: %s {\n". - " Class: %s\n". - " Allows NULL: %s\n". - " Passed to by reference: %s\n". - " Is optional?: %s\n". - "}\n", - $i, - $param->getName(), - var_export($param->getClass(), 1), - var_export($param->allowsNull(), 1), - var_export($param->isPassedByReference(), 1), - $param->isOptional() ? 'yes' : 'no' - ); - } +foreach ($reflect->getParameters() as $i => $param) { + printf( + "-- Parameter #%d: %s {\n". + " Class: %s\n". + " Allows NULL: %s\n". + " Passed to by reference: %s\n". + " Is optional?: %s\n". + "}\n", + $i, + $param->getName(), + var_export($param->getClass(), 1), + var_export($param->allowsNull(), 1), + var_export($param->isPassedByReference(), 1), + $param->isOptional() ? 'yes' : 'no' + ); +} ?> ]]> @@ -287,35 +287,36 @@ echo ReflectionFunction::export('counter'); ]]> @@ -330,85 +331,82 @@ echo ReflectionFunction::export('counter'); The %s%s%s %s '%s' [extends %s]\n". - " declared in %s\n". - " lines %d to %d\n". - " having the modifiers %d [%s]\n", - $class->isInternal() ? 'internal' : 'user-defined', - $class->isAbstract() ? ' abstract' : '', - $class->isFinal() ? ' final' : '', - $class->isInterface() ? 'interface' : 'class', - $class->getName(), - var_export($class->getParentClass(), 1), - $class->getFileName(), - $class->getStartLine(), - $class->getEndline(), - $class->getModifiers(), - implode(' ', Reflection::getModifierNames($class->getModifiers())) - ); +// Print out basic information +printf( + "===> The %s%s%s %s '%s' [extends %s]\n" . + " declared in %s\n" . + " lines %d to %d\n" . + " having the modifiers %d [%s]\n", + $class->isInternal() ? 'internal' : 'user-defined', + $class->isAbstract() ? ' abstract' : '', + $class->isFinal() ? ' final' : '', + $class->isInterface() ? 'interface' : 'class', + $class->getName(), + var_export($class->getParentClass(), 1), + $class->getFileName(), + $class->getStartLine(), + $class->getEndline(), + $class->getModifiers(), + implode(' ', Reflection::getModifierNames($class->getModifiers())) +); - // Print documentation comment - printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1)); +// Print documentation comment +printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1)); - // Print which interfaces are implemented by this class - printf("---> Implements:\n %s\n", var_export($class->getInterfaces(), 1)); +// Print which interfaces are implemented by this class +printf("---> Implements:\n %s\n", var_export($class->getInterfaces(), 1)); - // Print class constants - printf("---> Constants: %s\n", var_export($class->getConstants(), 1)); +// Print class constants +printf("---> Constants: %s\n", var_export($class->getConstants(), 1)); - // Print class properties - printf("---> Properties: %s\n", var_export($class->getProperties(), 1)); +// Print class properties +printf("---> Properties: %s\n", var_export($class->getProperties(), 1)); - // Print class methods - printf("---> Methods: %s\n", var_export($class->getMethods(), 1)); +// Print class methods +printf("---> Methods: %s\n", var_export($class->getMethods(), 1)); - // If this class is instantiable, create an instance - if ($class->isInstantiable()) - { - $counter= $class->newInstance(); +// If this class is instantiable, create an instance +if ($class->isInstantiable()) { + $counter= $class->newInstance(); - echo '---> $counter is instance? '; - echo $class->isInstance($counter) ? 'yes' : 'no'; + echo '---> $counter is instance? '; + echo $class->isInstance($counter) ? 'yes' : 'no'; - echo "\n---> new Object() is instance? "; - echo $class->isInstance(new Object()) ? 'yes' : 'no'; - } + echo "\n---> new Object() is instance? "; + echo $class->isInstance(new Object()) ? 'yes' : 'no'; +} ?> ]]> @@ -439,33 +437,34 @@ echo ReflectionFunction::export('counter'); ]]> @@ -480,61 +479,60 @@ echo ReflectionFunction::export('counter'); The %s%s%s%s%s%s%s method '%s' (which is %s)\n". - " declared in %s\n". - " lines %d to %d\n". +// Print out basic information +printf( + "===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" . + " declared in %s\n" . + " lines %d to %d\n" . " having the modifiers %d[%s]\n", - $method->isInternal() ? 'internal' : 'user-defined', - $method->isAbstract() ? ' abstract' : '', - $method->isFinal() ? ' final' : '', - $method->isPublic() ? ' public' : '', - $method->isPrivate() ? ' private' : '', - $method->isProtected() ? ' protected' : '', - $method->isStatic() ? ' static' : '', - $method->getName(), - $method->isConstructor() ? 'the constructor' : 'a regular method', - $method->getFileName(), - $method->getStartLine(), - $method->getEndline(), - $method->getModifiers(), - implode(' ', Reflection::getModifierNames($method->getModifiers())) - ); + $method->isInternal() ? 'internal' : 'user-defined', + $method->isAbstract() ? ' abstract' : '', + $method->isFinal() ? ' final' : '', + $method->isPublic() ? ' public' : '', + $method->isPrivate() ? ' private' : '', + $method->isProtected() ? ' protected' : '', + $method->isStatic() ? ' static' : '', + $method->getName(), + $method->isConstructor() ? 'the constructor' : 'a regular method', + $method->getFileName(), + $method->getStartLine(), + $method->getEndline(), + $method->getModifiers(), + implode(' ', Reflection::getModifierNames($method->getModifiers())) +); - // Print documentation comment - printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); +// Print documentation comment +printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); - // Print static variables if existant - if ($statics= $method->getStaticVariables()) - { - printf("---> Static variables: %s\n", var_export($statics, 1)); - } +// Print static variables if existant +if ($statics= $method->getStaticVariables()) { + printf("---> Static variables: %s\n", var_export($statics, 1)); +} - // Invoke the method - printf("---> Invokation results in: "); - var_dump($method->invoke(NULL)); +// Invoke the method +printf("---> Invokation results in: "); +var_dump($method->invoke(NULL)); ?> ]]> @@ -565,21 +563,22 @@ echo ReflectionFunction::export('counter'); ]]> @@ -594,41 +593,41 @@ echo ReflectionFunction::export('counter'); The%s%s%s%s property '%s' (which was %s)\n". - " having the modifiers %s\n", - $prop->isPublic() ? ' public' : '', - $prop->isPrivate() ? ' private' : '', - $prop->isProtected() ? ' protected' : '', - $prop->isStatic() ? ' static' : '', - $prop->getName(), - $prop->isDefault() ? 'declared at compile-time' : 'created at run-time', - var_export(Reflection::getModifierNames($prop->getModifiers()), 1) - ); +// Print out basic information +printf( + "===> The%s%s%s%s property '%s' (which was %s)\n" . + " having the modifiers %s\n", + $prop->isPublic() ? ' public' : '', + $prop->isPrivate() ? ' private' : '', + $prop->isProtected() ? ' protected' : '', + $prop->isStatic() ? ' static' : '', + $prop->getName(), + $prop->isDefault() ? 'declared at compile-time' : 'created at run-time', + var_export(Reflection::getModifierNames($prop->getModifiers()), 1) +); - // Create an instance of String - $obj= new String(); +// Create an instance of String +$obj= new String(); - // Get current value - printf("---> Value is: "); - var_dump($prop->getValue($obj)); +// Get current value +printf("---> Value is: "); +var_dump($prop->getValue($obj)); - // Change value - $prop->setValue($obj, 10); - printf("---> Setting value to 10, new value is: "); - var_dump($prop->getValue($obj)); +// Change value +$prop->setValue($obj, 10); +printf("---> Setting value to 10, new value is: "); +var_dump($prop->getValue($obj)); - // Dump object - var_dump($obj); +// Dump object +var_dump($obj); ?> ]]> @@ -652,16 +651,16 @@ echo ReflectionFunction::export('counter'); ]]> @@ -676,25 +675,25 @@ echo ReflectionFunction::export('counter'); getName(), - $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', - sizeof($ext->getFunctions()), - var_export($ext->getFunctions(), 1), - sizeof($ext->getConstants()), - var_export($ext->getConstants(), 1), - sizeof($ext->getINIEntries()), - var_export($ext->getINIEntries(), 1) - ); +// Print out basic information +printf( + "Name : %s\n" . + "Version : %s\n" . + "Functions : [%d] %s\n" . + "Constants : [%d] %s\n" . + "INI entries : [%d] %s\n", + $ext->getName(), + $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', + sizeof($ext->getFunctions()), + var_export($ext->getFunctions(), 1), + sizeof($ext->getConstants()), + var_export($ext->getConstants(), 1), + sizeof($ext->getINIEntries()), + var_export($ext->getINIEntries(), 1) +); ?> ]]> @@ -714,37 +713,38 @@ echo ReflectionFunction::export('counter'); visibility= Reflection::getModifierNames($this->getModifiers()); + public function __construct($o, $m) + { + parent::__construct($o, $m); + $this->visibility= Reflection::getModifierNames($this->getModifiers()); } - } +} - /** - * Demo class #1 - * - */ - class T { +/** + * Demo class #1 + * + */ +class T { protected function x() {} - } +} - /** - * Demo class #2 - * - */ - class U extends T { +/** + * Demo class #2 + * + */ +class U extends T { function x() {} - } +} - // Print out information - var_dump(new My_Reflection_Method('U', 'x')); +// Print out information +var_dump(new My_Reflection_Method('U', 'x')); ?> ]]> diff --git a/language/oop5/static.xml b/language/oop5/static.xml index d6ed9445a0..0cd4dcffe0 100644 --- a/language/oop5/static.xml +++ b/language/oop5/static.xml @@ -1,5 +1,5 @@ - + Static Keyword @@ -43,19 +43,20 @@ staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static -// $foo::my_static is not possible +// $foo::my_static is not possible print Bar::$my_static . "\n"; $bar = new Bar(); @@ -81,9 +82,9 @@ print $bar->fooStatic() . "\n";