Applied PEAR coding standards to examples

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@169794 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Aidan Lister 2004-10-02 09:40:52 +00:00
parent 5b435385c5
commit 6ae59bfbd5
13 changed files with 537 additions and 528 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<!-- $Revision: 1.8 $ -->
<sect1 id="language.oop5.abstract">
<title>Object Abstraction</title>
@ -23,32 +23,29 @@
<programlisting role="php">
<![CDATA[
<?php
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract class AbstractClass {
/* Force Extending class to define this method */
abstract protected function getValue();
/* Common method */
public function printOut() {
print $this->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";
}
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<!-- $Revision: 1.2 $ -->
<sect1 id="language.oop5.autoload">
<title>Autoloading Objects</title>
<para>
@ -26,8 +26,7 @@
<programlisting role="php">
<![CDATA[
<?php
function __autoload($class_name)
{
function __autoload($class_name) {
include_once($class_name . 'php');
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<sect1 id="language.oop5.basic">
<title>The Basics</title>
@ -21,14 +21,15 @@
<programlisting role="php">
<![CDATA[
<?php
class SimpleClass {
/* member declaration */
public $var = 'a default value';
class SimpleClass
{
// member declaration
public $var = 'a default value';
/* method declaration */
public function displayVar() {
echo $this->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) {
<programlisting role="php">
<![CDATA[
<?php
class ExtendClass extends SimpleClass {
/* Redefine the parent method */
function displayVar() {
echo "Extending class\n";
parent::displayVar();
}
class ExtendClass extends SimpleClass
{
// Redefine the parent method
function displayVar()
{
echo "Extending class\n";
parent::displayVar();
}
}
$extended = new ExtendClass();

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.3 $ -->
<!-- $Revision: 1.4 $ -->
<sect1 id="language.oop5.cloning">
<title>Object cloning</title>
@ -30,13 +30,11 @@ $copy_of_object = clone $object;
</informalexample>
<para>
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.
</para>
<example>
@ -44,31 +42,31 @@ $copy_of_object = clone $object;
<programlisting role="php">
<![CDATA[
<?php
class SubObject
{
static $instances = 0;
public $instance;
class SubObject {
static $instances = 0;
public $instance;
public function __construct() {
$this->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();

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<sect1 id="language.oop5.constants">
<title>Object Constants</title>
<para>
@ -15,19 +15,20 @@
<programlisting role="php">
<![CDATA[
<?php
class MyClass {
const constant = 'constant value';
class MyClass
{
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
function showConstant() {
echo self::constant . "\n";
}
}
echo MyClass::constant . "\n";
$class = new MyClass();
$class->showConstant();
/* echo $class::constant; is not allowed */
// echo $class::constant; is not allowed
?>
]]>
</programlisting>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.6 $ -->
<!-- $Revision: 1.7 $ -->
<sect1 id="language.oop5.exceptions">
<title>Exceptions</title>
@ -50,7 +50,8 @@ try {
<programlisting role="php">
<![CDATA[
<?php
class Exception {
class Exception
{
protected $message = 'Unknown exception'; // exception message
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
@ -88,8 +89,8 @@ class Exception {
/**
* Define a custom exception class
*/
class MyException extends Exception {
class MyException extends Exception
{
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0) {
// some code
@ -112,8 +113,8 @@ class MyException extends Exception {
/**
* Create a class to test the exception
*/
class TestException {
class TestException
{
public $var;
const THROW_NONE = 0;

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<sect1 id="language.oop5.iterations">
<title>Object Iteration</title>
<para>
@ -15,26 +15,24 @@
<![CDATA[
<?php
class MyClass {
public $var1 = 'value 1';
public $var2 = 'value 2';
public $var3 = 'value 3';
protected $protected = 'protected';
private $private = 'private';
class MyClass
{
public $var1 = 'value 1';
public $var2 = 'value 2';
public $var3 = 'value 3';
protected $protected = 'protected';
private $private = 'private';
}
$class = new MyClass();
foreach($class as $key => $value) {
print "$key => $value\n";
print "$key => $value\n";
}
]]>
</programlisting>
<para>
Will output:
</para>
&example.outputs;
<screen role="php">
<![CDATA[
var1 => value 1
@ -59,52 +57,52 @@ var3 => value 3
<programlisting role="php">
<![CDATA[
<?php
class MyIterator implements Iterator {
class MyIterator implements Iterator
{
private $var = array();
private $var = array();
public function __construct($array) {
if (is_array($array) ) {
$this->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";
}
]]>
</programlisting>
@ -150,19 +148,19 @@ valid:
<programlisting role="php">
<![CDATA[
<?php
class MyCollection implements IteratorAggregate {
private $items = array();
private $count = 0;
class MyCollection implements IteratorAggregate
{
private $items = array();
private $count = 0;
/* Required definition of interface IteratorAggregate */
public function getIterator() {
return new MyIterator($this->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";
}
?>

View file

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 id="language.oop5.magic">
<title>Magic Methods</title>
<para>
The function names
<literal>__construct</literal>,
<literal>__destruct</literal>,
<literal>__sleep</literal>,
<literal>__wakeup</literal>, and
<literal>__toString</literal>
@ -61,7 +63,8 @@
<![CDATA[
<?php
// Define a simple class
class TestClass {
class TestClass
{
public $foo;
public function __construct($foo) {

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 id="language.oop5.object-comparison">
<title>Comparing objects</title>
<para>
@ -25,34 +25,38 @@
<programlisting role='php'>
<![CDATA[
<?php
function bool2str($bool) {
function bool2str($bool)
{
if ($bool === false) {
return 'FALSE';
return 'FALSE';
} else {
return 'TRUE';
return 'TRUE';
}
}
function compareObjects(&$o1, &$o2) {
echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n";
echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n";
echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n";
echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n";
function compareObjects(&$o1, &$o2)
{
echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n";
echo 'o1 != o2 : ' . bool2str($o1 != $o2) . "\n";
echo 'o1 === o2 : ' . bool2str($o1 === $o2) . "\n";
echo 'o1 !== o2 : ' . bool2str($o1 !== $o2) . "\n";
}
class Flag {
var $flag;
class Flag
{
public $flag;
function Flag($flag=true) {
$this->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;
}
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- $Revision: 1.6 $ -->
<sect1 id="language.oop5.overloading">
<title>Overloading</title>
@ -36,11 +36,13 @@
<programlisting role="php">
<![CDATA[
<?php
class Setter {
class Setter
{
public $n;
private $x = array("a" => 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) {
<programlisting role="php">
<![CDATA[
<?php
class Caller {
class Caller
{
private $x = array(1, 2, 3);
function __call($m, $a) {
function __call($m, $a)
{
print "Method $m called:\n";
var_dump($a);
return $this->x;

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.6 $ -->
<!-- $Revision: 1.7 $ -->
<sect1 id="language.oop5.paamayim-nekudotayim">
<title>Scope Resolution Operator (::)</title>
@ -29,8 +29,9 @@
<![CDATA[
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
const CONST_VALUE = 'A constant value';
}
echo MyClass::CONST_VALUE;
?>
]]>
@ -47,13 +48,14 @@ echo MyClass::CONST_VALUE;
<programlisting role="php">
<![CDATA[
<?php
class OtherClass extends MyClass {
public static $my_static = 'static var';
class OtherClass extends MyClass
{
public static $my_static = 'static var';
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";
}
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";
}
}
OtherClass::doubleColon();
@ -76,22 +78,22 @@ OtherClass::doubleColon();
<programlisting role="php">
<![CDATA[
<?php
class MyClass {
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
}
class OtherClass extends MyClass {
/* Override parent's definition */
public function myFunc() {
/* But still call the parent function */
parent::myFunc();
echo "OtherClass::myFunc()\n";
}
class OtherClass extends MyClass
{
// Override parent's definition
public function myFunc()
{
// But still call the parent function
parent::myFunc();
echo "OtherClass::myFunc()\n";
}
}
$class = new OtherClass();

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 id="language.oop5.reflection">
<title>Reflection</title>
<sect2 id="language.oop5.reflection.introduction">
@ -18,16 +18,16 @@
<programlisting role="php">
<![CDATA[
<?php
class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }
class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }
?>
]]>
</programlisting>
@ -44,12 +44,12 @@
<programlisting role='php'>
<![CDATA[
<?php
Reflection::export(new ReflectionClass('Exception'));
Reflection::export(new ReflectionClass('Exception'));
?>
]]>
</programlisting>
</example>
We will see:
&example.outputs;
<screen>
<![CDATA[
Class [ <internal> class Exception ] {
@ -116,22 +116,23 @@ Class [ <internal> class Exception ] {
<programlisting role="php">
<![CDATA[
<?php
class ReflectionFunction implements Reflector {
public object __construct(string name)
public string __toString()
public static string export()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public mixed invoke(mixed* args)
public bool returnsReference()
public ReflectionParameter[] getParameters()
}
class ReflectionFunction implements Reflector
{
public object __construct(string name)
public string __toString()
public static string export()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public mixed invoke(mixed* args)
public bool returnsReference()
public ReflectionParameter[] getParameters()
}
?>
]]>
</programlisting>
@ -154,7 +155,6 @@ Class [ <internal> class Exception ] {
function counter()
{
static $c = 0;
return $c++;
}
@ -213,16 +213,17 @@ echo ReflectionFunction::export('counter');
<programlisting role="php">
<![CDATA[
<?php
class ReflectionParameter implements Reflector {
public object __construct(string name)
public string __toString()
public static string export()
public string getName()
public ReflectionClass getClass()
public bool allowsNull()
public bool isPassedByReference()
public bool isOptional()
}
class ReflectionParameter implements Reflector
{
public object __construct(string name)
public string __toString()
public static string export()
public string getName()
public ReflectionClass getClass()
public bool allowsNull()
public bool isPassedByReference()
public bool isOptional()
}
?>
]]>
</programlisting>
@ -243,34 +244,33 @@ echo ReflectionFunction::export('counter');
<programlisting role='php'>
<![CDATA[
<?php
function foo($a, $b, $c) { }
function bar(Exception $a, &$b, $c) { }
function baz(ReflectionFunction $a, $b = 1, $c = null) { }
function abc() { }
function foo($a, $b, $c) { }
function bar(Exception $a, &$b, $c) { }
function baz(ReflectionFunction $a, $b = 1, $c = null) { }
function abc() { }
// Create an instance of Reflection_Function with the
// parameter given from the command line.
$reflect = new ReflectionFunction($argv[1]);
// Create an instance of Reflection_Function with the
// parameter given from the command line.
$reflect = new ReflectionFunction($argv[1]);
echo $reflect;
echo $reflect;
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'
);
}
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'
);
}
?>
]]>
</programlisting>
@ -287,35 +287,36 @@ echo ReflectionFunction::export('counter');
<programlisting role="php">
<![CDATA[
<?php
class ReflectionClass implements Reflector {
public __construct(string name)
public string __toString()
public static string export()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public ReflectionMethod getConstructor()
public ReflectionMethod getMethod(string name)
public ReflectionMethod[] getMethods()
public ReflectionProperty getProperty(string name)
public ReflectionProperty[] getProperties()
public array getConstants()
public mixed getConstant(string name)
public bool isInstantiable()
public bool isInterface()
public bool isFinal()
public bool isAbstract()
public int getModifiers()
public bool isInstance(stdclass object)
public stdclass newInstance(mixed* args)
public ReflectionClass[] getInterfaces()
public ReflectionClass getParentClass()
public bool isSubclassOf(ReflectionClass class)
}
class ReflectionClass implements Reflector
{
public __construct(string name)
public string __toString()
public static string export()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public ReflectionMethod getConstructor()
public ReflectionMethod getMethod(string name)
public ReflectionMethod[] getMethods()
public ReflectionProperty getProperty(string name)
public ReflectionProperty[] getProperties()
public array getConstants()
public mixed getConstant(string name)
public bool isInstantiable()
public bool isInterface()
public bool isFinal()
public bool isAbstract()
public int getModifiers()
public bool isInstance(stdclass object)
public stdclass newInstance(mixed* args)
public ReflectionClass[] getInterfaces()
public ReflectionClass getParentClass()
public bool isSubclassOf(ReflectionClass class)
}
?>
]]>
</programlisting>
@ -330,85 +331,82 @@ echo ReflectionFunction::export('counter');
<programlisting role='php'>
<![CDATA[
<?php
interface Serializable
{
// ...
}
interface Serializable
{
// ...
}
class Object
{
// ...
}
class Object
{
// ...
}
/**
* A counter class
*
*/
class Counter extends Object implements Serializable
{
const START = 0;
private static $c = Counter::START;
/**
* A counter class
*/
class Counter extends Object implements Serializable
{
const START = 0;
private static $c = Counter::START;
/**
* Invoke counter
*
* @access public
* @return int
*/
public function count()
{
return self::$c++;
}
}
/**
* Invoke counter
*
* @access public
* @return int
*/
public function count() {
return self::$c++;
}
}
// Create an instance of the ReflectionClass class
$class= new ReflectionClass('Counter');
// Create an instance of the ReflectionClass class
$class= new ReflectionClass('Counter');
// 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 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';
}
?>
]]>
</programlisting>
@ -439,33 +437,34 @@ echo ReflectionFunction::export('counter');
<programlisting role="php">
<![CDATA[
<?php
class ReflectionMethod extends ReflectionFunction {
public __construct(mixed class, string name)
public static string export()
public mixed invoke(stdclass object, mixed* args)
public bool isFinal()
public bool isAbstract()
public bool isPublic()
public bool isPrivate()
public bool isProtected()
public bool isStatic()
public bool isConstructor()
public int getModifiers()
public ReflectionClass getDeclaringClass()
class ReflectionMethod extends ReflectionFunction
{
public __construct(mixed class, string name)
public static string export()
public mixed invoke(stdclass object, mixed* args)
public bool isFinal()
public bool isAbstract()
public bool isPublic()
public bool isPrivate()
public bool isProtected()
public bool isStatic()
public bool isConstructor()
public int getModifiers()
public ReflectionClass getDeclaringClass()
/* Inherited from ReflectionFunction */
public string __toString()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public bool returnsReference()
public ReflectionParameter[] getParameters()
}
// Inherited from ReflectionFunction
public string __toString()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public bool returnsReference()
public ReflectionParameter[] getParameters()
}
?>
]]>
</programlisting>
@ -480,61 +479,60 @@ echo ReflectionFunction::export('counter');
<programlisting role='php'>
<![CDATA[
<?php
class Counter {
private static $c = 0;
class Counter
{
private static $c = 0;
/**
* Increment counter
*
* @final
* @static
* @access public
* @return int
*/
final public static function increment()
{
self::$c++;
return self::$c;
}
}
/**
* Increment counter
*
* @final
* @static
* @access public
* @return int
*/
final public static function increment()
{
return ++self::$c;
}
}
// Create an instance of the Reflection_Method class
$method= new ReflectionMethod('Counter', 'increment');
// Create an instance of the Reflection_Method class
$method= new ReflectionMethod('Counter', 'increment');
// 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".
// 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));
?>
]]>
</programlisting>
@ -565,21 +563,22 @@ echo ReflectionFunction::export('counter');
<programlisting role="php">
<![CDATA[
<?php
class ReflectionProperty implements Reflector {
public __construct(mixed class, string name)
public string __toString()
public static string export()
public string getName()
public bool isPublic()
public bool isPrivate()
public bool isProtected()
public bool isStatic()
public bool isDefault()
public int getModifiers()
public mixed getValue(stdclass object)
public void setValue(stdclass object, mixed value)
public ReflectionClass getDeclaringClass()
}
class ReflectionProperty implements Reflector
{
public __construct(mixed class, string name)
public string __toString()
public static string export()
public string getName()
public bool isPublic()
public bool isPrivate()
public bool isProtected()
public bool isStatic()
public bool isDefault()
public int getModifiers()
public mixed getValue(stdclass object)
public void setValue(stdclass object, mixed value)
public ReflectionClass getDeclaringClass()
}
?>
]]>
</programlisting>
@ -594,41 +593,41 @@ echo ReflectionFunction::export('counter');
<programlisting role='php'>
<![CDATA[
<?php
class String
{
public $length = 5;
}
class String
{
public $length = 5;
}
// Create an instance of the ReflectionProperty class
$prop = new ReflectionProperty('String', 'length');
// Create an instance of the ReflectionProperty class
$prop = new ReflectionProperty('String', 'length');
// 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)
);
// 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);
?>
]]>
</programlisting>
@ -652,16 +651,16 @@ echo ReflectionFunction::export('counter');
<programlisting role="php">
<![CDATA[
<?php
class ReflectionExtension implements Reflector {
public __construct(string name)
public string __toString()
public static string export()
public string getName()
public string getVersion()
public ReflectionFunction[] getFunctions()
public array getConstants()
public array getINIEntries()
}
class ReflectionExtension implements Reflector {
public __construct(string name)
public string __toString()
public static string export()
public string getName()
public string getVersion()
public ReflectionFunction[] getFunctions()
public array getConstants()
public array getINIEntries()
}
?>
]]>
</programlisting>
@ -676,25 +675,25 @@ echo ReflectionFunction::export('counter');
<programlisting role='php'>
<![CDATA[
<?php
// Create an instance of the ReflectionProperty class
$ext = new ReflectionExtension('standard');
// Create an instance of the ReflectionProperty class
$ext = new ReflectionExtension('standard');
// 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)
);
// 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)
);
?>
]]>
</programlisting>
@ -714,37 +713,38 @@ echo ReflectionFunction::export('counter');
<programlisting role='php'>
<![CDATA[
<?php
/**
* My Reflection_Method class
*
*/
class My_Reflection_Method extends ReflectionMethod {
public $visibility= '';
/**
* My Reflection_Method class
*/
class My_Reflection_Method extends ReflectionMethod
{
public $visibility = '';
public function __construct($o, $m) {
parent::__construct($o, $m);
$this->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'));
?>
]]>
</programlisting>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<sect1 id="language.oop5.static">
<title>Static Keyword</title>
@ -43,19 +43,20 @@
<programlisting role="php">
<![CDATA[
<?php
class Foo {
public static $my_static = 'foo';
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo {
public function fooStatic() {
return parent::$my_static;
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
@ -65,7 +66,7 @@ $foo = new Foo();
print $foo->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";
<![CDATA[
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();