nlopess Sun Jul 11 08:33:25 2004 EDT
Added files:
/phpdoc/en/language/oop5 abstract.xml cloning.xml constants.xml
decon.xml final.xml interfaces.xml
iterations.xml magic.xml
object-comparison.xml overloading.xml
paamayim-nekudotayim.xml reflection.xml
static.xml visibility.xml
Removed files:
/phpdoc/en/language/oop5 oop5-abstract.xml oop5-cloning.xml
oop5-constants.xml oop5-decon.xml
oop5-final.xml oop5-interfaces.xml
oop5-iterations.xml oop5-magic.xml
oop5-object-comparison.xml
oop5-overloading.xml
oop5-paamayim-nekudotayim.xml
oop5-reflection.xml oop5-static.xml
oop5-visibility.xml
Modified files:
/phpdoc/en/language oop5.xml
Log:
renaming files and adding initial reflection documentation
http://cvs.php.net/diff.php/phpdoc/en/language/oop5.xml?r1=1.4&r2=1.5&ty=u
Index: phpdoc/en/language/oop5.xml
diff -u phpdoc/en/language/oop5.xml:1.4 phpdoc/en/language/oop5.xml:1.5
--- phpdoc/en/language/oop5.xml:1.4 Sun Jul 11 06:06:18 2004
+++ phpdoc/en/language/oop5.xml Sun Jul 11 08:33:25 2004
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.4 $ -->
+<!-- $Revision: 1.5 $ -->
<chapter id="language.oop5">
<title>Classes and Objects (PHP 5)</title>
@@ -10,20 +10,20 @@
</para>
</sect1>
- &language.oop5.oop5-decon;
- &language.oop5.oop5-visibility;
- &language.oop5.oop5-paamayim-nekudotayim;
- &language.oop5.oop5-static;
- &language.oop5.oop5-constants;
- &language.oop5.oop5-abstract;
- &language.oop5.oop5-interfaces;
- &language.oop5.oop5-overloading;
- &language.oop5.oop5-iterations;
- &language.oop5.oop5-magic;
- &language.oop5.oop5-final;
- &language.oop5.oop5-cloning;
- &language.oop5.oop5-object-comparison;
- &language.oop5.oop5-reflection;
+ &language.oop5.decon;
+ &language.oop5.visibility;
+ &language.oop5.paamayim-nekudotayim;
+ &language.oop5.static;
+ &language.oop5.constants;
+ &language.oop5.abstract;
+ &language.oop5.interfaces;
+ &language.oop5.overloading;
+ &language.oop5.iterations;
+ &language.oop5.magic;
+ &language.oop5.final;
+ &language.oop5.cloning;
+ &language.oop5.object-comparison;
+ &language.oop5.reflection;
</chapter>
<!-- Keep this comment at the end of the file
http://cvs.php.net/co.php/phpdoc/en/language/oop5/abstract.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/abstract.xml
+++ phpdoc/en/language/oop5/abstract.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.abstract">
<title>Object Abstraction</title>
<para>
PHP 5 introduces abstract classes and methods. An abstract
method only declares the method's signature and does not provide an
implementation. A class that contains abstract methods needs to be declared
abstract.
</para>
<example>
<title></title>
<programlisting role="php">
<![CDATA[
<?php
abstract class AbstractClass {
abstract public function test();
}
class ImplementedClass extends AbstractClass {
public function test() {
echo "ImplementedClass::test() called.\n";
}
}
$o = new ImplementedClass;
$o->test();
?>
]]>
</programlisting>
</example>
<para>
Abstract classes cannot be instantiated. Old code that has no user-defined
classes or functions named 'abstract' should run without modifications.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/cloning.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/cloning.xml
+++ phpdoc/en/language/oop5/cloning.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.cloning">
<title>Object cloning</title>
<para>
Creating a copy of an object with fully replicated properties is not
always the wanted behavior. A good example of the need for copy
constructors, is if you have an object which represents a GTK window and the
object holds the resource of this GTK window, when you create a duplicate
you might want to create a new window with the same properties and have the
new object hold the resource of the new window. Another example is if your
object holds a reference to another object which it uses and when you
replicate the parent object you want to create a new instance of this other
object so that the replica has its own separate copy.
</para>
<para>
An object copy is created by using the clone keyword (which calls the
object's __clone() method if possible). An object's __clone() method
cannot be called directly.
</para>
<informalexample>
<programlisting>
<![CDATA[
$copy_of_object = clone $object;
]]>
</programlisting>
</informalexample>
<para>
When the developer asks to create a new copy of an object, PHP 5 will check
if a __clone() method has been defined or not. If not, it will call a
default __clone() which will copy all of the object's properties. If a
__clone() method is defined, then it will be responsible to set the
necessary properties in the created object. For convenience, the engine
will supply a function that imports all of the properties from the source
object, so that they can start with a by-value replica of the source
object, and only override properties that need to be changed.
</para>
<example>
<title>Cloning an object</title>
<programlisting role="php">
<![CDATA[
<?php
class MyCloneable {
static $id = 0;
function MyCloneable() {
$this->id = self::$id++;
}
function __clone() {
$this->address = "New York";
$this->id = self::$id++;
}
}
$obj = new MyCloneable();
$obj->name = "Hello";
$obj->address = "Tel-Aviv";
print $obj->id . "\n";
$obj_cloned = clone $obj;
print $obj_cloned->id . "\n";
print $obj_cloned->name . "\n";
print $obj_cloned->address . "\n";
?>
]]>
</programlisting>
</example>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/constants.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/constants.xml
+++ phpdoc/en/language/oop5/constants.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.constants">
<title>Object Constants</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/decon.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/decon.xml
+++ phpdoc/en/language/oop5/decon.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.decon">
<title>Constructors and Destructors</title>
<sect2 id="oop5-decon-constructor">
<title>Constructor</title>
<para>
PHP 5 allows developers to declare constructor methods for classes.
Classes which have a constructor method call this method on each
newly-created object, so it is suitable for any initialization that the
object may need before it is used.
</para>
<note>
<simpara>
Parent constructors are not called implicitly. In order to run
a parent constructor, a call to
<function>parent::__construct</function> is required.
</simpara>
</note>
<example>
<title>using new unified constructors</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
function __construct() {
print "In BaseClass constructor\n";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructor\n";
}
}
$obj = new BaseClass();
$obj = new SubClass();
?>
]]>
</programlisting>
</example>
<para>
For backwards compatibility, if PHP 5 cannot find a
<function>__construct</function> function for a given class, it will
search for the old-style constructor function, by the name of the class.
Effectively, it means that the only case that would have compatibility
issues is if the class had a method named
<function>__construct</function> which was used for different semantics.
</para>
</sect2>
<sect2 id="oop5-decon-destructor">
<title>Destructor</title>
<para>
PHP 5 introduces a destructor concept similar to that of other
object-oriented languages, such as Java: When the last reference to an
object is destroyed the object's destructor, which is a class method
named <function>__destruct</function> that receives no parameters, is
called before the object is freed from memory.
</para>
<example>
<title>Destructor Example</title>
<programlisting role="php">
<![CDATA[
<?php
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>
]]>
</programlisting>
</example>
<para>
Like constructors, parent destructors will not be called implicitly by
the engine. In order to run a parent destructor, one would have to
explicitly call <function>parent::__destruct</function> in the destructor
body.
</para>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/final.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/final.xml
+++ phpdoc/en/language/oop5/final.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.final">
<title>Final Keyword</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/interfaces.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/interfaces.xml
+++ phpdoc/en/language/oop5/interfaces.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.interfaces">
<title>Object Interfaces</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/iterations.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/iterations.xml
+++ phpdoc/en/language/oop5/iterations.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.iterations">
<title>Object Iteration</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/magic.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/magic.xml
+++ phpdoc/en/language/oop5/magic.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.magic">
<title>Magic Methods</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/object-comparison.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/object-comparison.xml
+++ phpdoc/en/language/oop5/object-comparison.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.object-comparison">
<title>Comparing objects</title>
<para>
In PHP 5, object comparison is a more complicated than in PHP 4 and more
in accordance to what one will expect from an Object Oriented Language
(not that PHP 5 is such a language).
</para>
<para>
When using the comparison operator (<literal>==</literal>),
object variables are compared in a simple manner, namely: Two object
instances are equal if they have the same attributes and values, and are
instances of the same class.
</para>
<para>
On the other hand, when using the identity operator (<literal>===</literal>),
object variables are identical if and only if they refer to the same
instance of the same class.
</para>
<para>
An example will clarify these rules.
<example>
<title>Example of object comparison in PHP 5</title>
<programlisting role='php'>
<![CDATA[
<?php
function bool2str($bool) {
if ($bool === false) {
return 'FALSE';
} else {
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";
}
class Flag {
var $flag;
function Flag($flag=true) {
$this->flag = $flag;
}
}
class OtherFlag {
var $flag;
function OtherFlag($flag=true) {
$this->flag = $flag;
}
}
$o = new Flag();
$p = new Flag();
$q = $o;
$r = new OtherFlag();
echo "Two instances of the same class\n";
compareObjects($o, $p);
echo "\nTwo references to the same instance\n";
compareObjects($o, $q);
echo "\nInstances of two different classes\n";
compareObjects($o, $r);
?>
]]>
</programlisting>
</example>
This example will output:
<screen>
Two instances of the same class
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : FALSE
o1 !== o2 : TRUE
Two references to the same instance
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE
Instances of two different classes
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE
</screen>
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/overloading.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/overloading.xml
+++ phpdoc/en/language/oop5/overloading.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.overloading">
<title>Overloading</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/paamayim-nekudotayim.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/paamayim-nekudotayim.xml
+++ phpdoc/en/language/oop5/paamayim-nekudotayim.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.paamayim-nekudotayim">
<title>::</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/reflection.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/reflection.xml
+++ phpdoc/en/language/oop5/reflection.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.reflection">
<title>Reflection</title>
<sect2 id="language.oop5.reflection.introduction">
<title>Introduction</title>
<para>
PHP 5 comes with a complete reflection API that adds the ability to
reverse-engineer classes, interfaces, functions and methods as well
as extensions. Additionally, the reflection API also offers ways of
retrieving doc comments for functions, classes and methods.
</para>
<para>
The reflection API is an object-oriented extension to the Zend Engine,
consisting of the following classes:
</para>
<informalexample>
<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 { }
?>
]]>
</programlisting>
</informalexample>
<note>
<simpara>
For details on these classes, have a look at the next chapters.
</simpara>
</note>
<para>
If we were to execute the code in the example below:
<example>
<title>Basic usage of the reflection API</title>
<programlisting role='php'>
<![CDATA[
<?php
Reflection::export(new ReflectionClass('Exception'));
?>
]]>
</programlisting>
</example>
We will see:
<screen>
<![CDATA[
Class [ <internal> class Exception ] {
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [6] {
Property [ <default> protected $message ]
Property [ <default> private $string ]
Property [ <default> protected $code ]
Property [ <default> protected $file ]
Property [ <default> protected $line ]
Property [ <default> private $trace ]
}
- Methods [9] {
Method [ <internal> final private method __clone ] {
}
Method [ <internal> <ctor> method __construct ] {
}
Method [ <internal> final public method getMessage ] {
}
Method [ <internal> final public method getCode ] {
}
Method [ <internal> final public method getFile ] {
}
Method [ <internal> final public method getLine ] {
}
Method [ <internal> final public method getTrace ] {
}
Method [ <internal> final public method getTraceAsString ] {
}
Method [ <internal> public method __toString ] {
}
}
}
]]>
</screen>
</para>
</sect2>
<sect2 id="language.oop5.reflection.reflectionfunction">
<title><classname>ReflectionFunction</classname></title>
<para>
The <classname>ReflectionFunction</classname> class lets you
reverse-engineer functions.
</para>
<informalexample>
<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()
}
?>
]]>
</programlisting>
</informalexample>
<para>
To introspect a function, you will first have to create an instance
of the <classname>ReflectionFunction</classname> class. You can then call
any of the above methods on this instance.
</para>
<example>
<title>Using the <classname>ReflectionFunction</classname> class</title>
<programlisting role='php'>
<![CDATA[
<?php
/**
* A simple counter
*
* @return int
*/
function counter()
{
static $c = 0;
return $c++;
}
// Create an instance of the Reflection_Function class
$func = new ReflectionFunction('counter');
// Print out basic information
printf(
"===> The %s function '%s'\n".
" declared in %s\n".
" lines %d to %d\n",
$func->isInternal() ? 'internal' : 'user-defined',
$func->getName(),
$func->getFileName(),
$func->getStartLine(),
$func->getEndline()
);
// Print documentation comment
printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1));
// Print static variables if existant
if ($statics = $func->getStaticVariables())
{
printf("---> Static variables: %s\n", var_export($statics, 1));
}
// Invoke the function
printf("---> Invokation results in: ");
var_dump($func->invoke());
// you may prefer to use the export() method
echo "\nReflectionFunction::export() results:\n";
echo ReflectionFunction::export('counter');
?>
]]>
</programlisting>
</example>
<note>
<simpara>
The method <function>invoke</function> accepts a variable number of
arguments which are passed to the function just as in
<function>call_user_func</function>.
</simpara>
</note>
</sect2>
<sect2 id="language.oop5.reflection.reflectionparameter">
<title><classname>ReflectionParameter</classname></title>
<para>
The <classname>ReflectionParameter</classname> class retrieves
information about a function's or method's parameters.
</para>
<informalexample>
<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()
}
?>
]]>
</programlisting>
</informalexample>
<para>
To introspect function parameters, you will first have to create an instance
of the <classname>ReflectionFunction</classname> or
<classname>ReflectionMethod</classname> classes and then use their
<function>getParameters</function> method to retrieve an array of parameters.
</para>
<example>
<title>Using the <classname>ReflectionParameter</classname> class</title>
<programlisting role='php'>
<![CDATA[
<?php
function foo($a, $b, $c) { }
function bar(Exception $a, &$b, $c) { }
function baz($a = 1, $b = NULL, ReflectionFunction $c) { }
function abc() { }
// Create an instance of Reflection_Function with the
// parameter given from the command line.
$reflect = new ReflectionFunction($argv[1]);
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".
"}\n",
$i,
$param->getName(),
var_export($param->getClass(), 1),
var_export($param->allowsNull(), 1),
var_export($param->isPassedByReference(), 1)
);
}
?>
]]>
</programlisting>
</example>
</sect2>
<sect2 id="language.oop5.reflection.reflectionclass">
<title><classname>ReflectionClass</classname></title>
<para>
The <classname>ReflectionClass</classname> class lets
you reverse-engineer classes.
</para>
<informalexample>
<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)
}
?>
]]>
</programlisting>
</informalexample>
<para>
To introspect a class, you will first have to create an instance
of the <classname>ReflectionClass</classname> class. You can then
call any of the above methods on this instance.
</para>
<example>
<title>Using the <classname>ReflectionClass</classname> class</title>
<programlisting role='php'>
<![CDATA[
<?php
interface Serializable
{
// ...
}
class Object
{
// ...
}
/**
* 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++;
}
}
// 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 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 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 methods
printf("---> Methods: %s\n", var_export($class->getMethods(), 1));
// 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 "\n---> new Object() is instance? ";
echo $class->isInstance(new Object()) ? 'yes' : 'no';
}
?>
]]>
</programlisting>
</example>
<note>
<simpara>
The method <function>newInstance</function> accepts a variable number of
arguments which are passed to the function just as in
<function>call_user_func</function>.
</simpara>
</note>
<note>
<simpara>
<literal>$class = new ReflectionClass('Foo');
$class->isInstance($arg)</literal>
is equivalent to <literal>$arg instanceof Foo</literal> or
<literal>is_a($arg, 'Foo')</literal>.
</simpara>
</note>
</sect2>
<sect2 id="language.oop5.reflection.reflectionmethod">
<title><classname>ReflectionMethod</classname></title>
<para>
The <classname>ReflectionMethod</classname> class lets you
reverse-engineer class methods.
</para>
<informalexample>
<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()
/* 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>
</informalexample>
<para>
To introspect a method, you will first have to create an instance
of the <classname>ReflectionMethod</classname> class. You can then call
any of the above methods on this instance.
</para>
<example>
<title>Using the <classname>ReflectionMethod</classname> class</title>
<programlisting role='php'>
<![CDATA[
<?php
class Counter {
private static $c = 0;
/**
* Increment counter
*
* @final
* @static
* @access public
* @return int
*/
final public static function increment()
{
self::$c++;
return self::$c;
}
}
// 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".
" 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()))
);
// 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));
}
// Invoke the method
printf("---> Invokation results in: ");
var_dump($method->invoke(NULL));
?>
]]>
</programlisting>
</example>
<note>
<simpara>
Trying to invoke private, protected or abstract methods will result
in an exception being thrown from the <function>invoke</function>
method.
</simpara>
</note>
<note>
<simpara>
For static methods as seen above, you should pass NULL as the first
argument to <function>invoke</function>. For non-static methods, pass
an instance of the class.
</simpara>
</note>
</sect2>
<sect2 id="language.oop5.reflection.reflectionproperty">
<title><classname>ReflectionProperty</classname></title>
<para>
The <classname>ReflectionProperty</classname> class lets you
reverse-engineer class properties.
</para>
<informalexample>
<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()
}
?>
]]>
</programlisting>
</informalexample>
<para>
To introspect a method, you will first have to create an instance
of the <classname>ReflectionProperty</classname> class. You can then
call any of the above methods on this instance.
</para>
<example>
<title>Using the <classname>ReflectionProperty</classname> class</title>
<programlisting role='php'>
<![CDATA[
<?php
class String
{
public $length = 5;
}
// 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)
);
// Create an instance of String
$obj= new String();
// 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));
// Dump object
var_dump($obj);
?>
]]>
</programlisting>
</example>
<note>
<simpara>
Trying to get or set private or protected class property's values
will result in an exception being thrown.
</simpara>
</note>
</sect2>
<sect2 id="language.oop5.reflection.reflectionextension">
<title><classname>ReflectionExtension</classname></title>
<para>
The <classname>ReflectionExtension</classname> class lets you
reverse-engineer extensions. You can retrieve all loaded extensions
at runtime using the <function>get_loaded_extensions</function>.
</para>
<informalexample>
<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()
}
?>
]]>
</programlisting>
</informalexample>
<para>
To introspect a method, you will first have to create an instance
of the <classname>ReflectionProperty</classname> class. You can then call
any of the above methods on this instance.
</para>
<example>
<title>Using the <classname>ReflectionExtension</classname> class</title>
<programlisting role='php'>
<![CDATA[
<?php
// 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)
);
?>
]]>
</programlisting>
</example>
</sect2>
<sect2 id="language.oop5.reflection.extending">
<title>Extending the reflection classes</title>
<para>
In case you want to create specialized versions of the built-in
classes (say, for creating colorized HTML when being exported,
having easy-access member variables instead of methods or
having utility methods), you may go ahead and extend them.
</para>
<example>
<title>Extending the built-in classes</title>
<programlisting role='php'>
<![CDATA[
<?php
/**
* 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());
}
}
/**
* Demo class #1
*
*/
class T {
protected function x() {}
}
/**
* Demo class #2
*
*/
class U extends T {
function x() {}
}
// Print out information
var_dump(new My_Reflection_Method('U', 'x'));
?>
]]>
</programlisting>
</example>
<note>
<simpara>
Caution: If you're overwriting the constructor, remember to call
the parent's constructor _before_ any code you insert. Failing to
do so will result in the following:
<literal>
Fatal error: Internal error: Failed to retrieve the reflection object
</literal>
</simpara>
</note>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/static.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/static.xml
+++ phpdoc/en/language/oop5/static.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.static">
<title>Static</title>
<para>
.
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
http://cvs.php.net/co.php/phpdoc/en/language/oop5/visibility.xml?r=1.1&p=1
Index: phpdoc/en/language/oop5/visibility.xml
+++ phpdoc/en/language/oop5/visibility.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<sect1 id="language.oop5.visibility">
<title>Visibility</title>
<para>
The visibility of a member or method can be defined by prefixing the
declaration with the keywords: public, protected or private. Public
declared items can be allow access to any caller. Protected limits access
access to only classes inherited. Protected limits visiblity only to the
class that defines the item.
</para>
<sect2 id="language.oop5.visiblity-members">
<title>Members Visibility</title>
<para>
Class members must be defined with public, private, or private.
</para>
<example>
<title>Member declaration</title>
<programlisting role="php">
<![CDATA[
<?php
class MyClass {
public $public = "MyClass::public!\n";
protected $protected = "MyClass::Protected!\n";
protected $protected2 = "MyClass::Protected2!\n";
private $private = "MyClass::private!\n";
function printHello() {
print "MyClass::printHello() " . $this->private;
print "MyClass::printHello() " . $this->protected;
print "MyClass::printHello() " . $this->protected2;
}
}
class MyClass2 extends MyClass {
protected $protected = "MyClass2::protected!\n";
function printHello() {
MyClass::printHello();
print "MyClass2::printHello() " . $this->public;
print "MyClass2::printHello() " . $this->protected;
print "MyClass2::printHello() " . $this->protected2;
/* Will result in a Fatal Error: */
//print "MyClass2::printHello() " . $this->private; /* Fatal Error */
}
}
$obj = new MyClass();
print "Main:: " . $obj->public;
//print $obj->private; /* Fatal Error */
//print $obj->protected; /* Fatal Error */
//print $obj->protected2; /* Fatal Error */
$obj->printHello(); /* Should print */
$obj2 = new MyClass2();
print "Main:: " . $obj2->private; /* Undefined */
//print $obj2->protected; /* Fatal Error */
//print $obj2->protected2; /* Fatal Error */
$obj2->printHello();
?>
]]>
</programlisting>
</example>
<note>
<simpara>
The use PHP 4 use of declaring a variable with the keyword 'var' is
no longer valid for PHP 5 objects. For compatiblity a variable declared
in php will be assumed with public visiblity, and a E_STRICT warning will
be issued.
</simpara>
</note>
</sect2>
<sect2 id="language.oop5.visiblity-methods">
<title>Method Visibility</title>
<para>
.
</para>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->