svn: /phpdoc/en/trunk/ appendices/migration70/new-features.xml language/oop5/anonymous.xml language/oop5.xml

From: Date: Sun, 09 Aug 2015 01:17:17 +0000
Subject: svn: /phpdoc/en/trunk/ appendices/migration70/new-features.xml language/oop5/anonymous.xml language/oop5.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-13553@lists.php.net to get a copy of this message
aharvey Sun, 09 Aug 2015 01:17:17 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=337370 Log: Move the (excellent!) anonymous class documentation to the language reference. Changed paths: U phpdoc/en/trunk/appendices/migration70/new-features.xml A phpdoc/en/trunk/language/oop5/anonymous.xml U phpdoc/en/trunk/language/oop5.xml Modified: phpdoc/en/trunk/appendices/migration70/new-features.xml =================================================================== --- phpdoc/en/trunk/appendices/migration70/new-features.xml 2015-08-09 01:17:12 UTC (rev 337369) +++ phpdoc/en/trunk/appendices/migration70/new-features.xml 2015-08-09 01:17:17 UTC (rev 337370) @@ -202,129 +202,62 @@ </programlisting> </informalexample> </sect2> + <sect2 xml:id="migration70.new-features.anonymous-classes"> - <title>Anonymous Classes</title> + <title>Anonymous classes</title> <para> - Anonymous classes are useful when simple, one-off objects need to be created. + Support for anonymous classes has been added via <literal>new + class</literal>. These can be used in place of full class definitions for + throwaway objects: </para> <informalexample> <programlisting role="php"> <![CDATA[ <?php - -// Pre PHP 7 code -class Logger -{ - public function log($msg) - { - echo $msg; - } +interface Logger { + public function log(string $msg); } -$util->setLogger(new Logger()); +class Application { + private $logger; -// PHP 7+ code -$util->setLogger(new class { - public function log($msg) - { - echo $msg; + public function getLogger(): Logger { + return $this->logger; } -}); -]]> - </programlisting> - </informalexample> - <para> - They can pass arguments through to their constructors, extend other classes, - implement interfaces, and use traits just like a normal class can: - </para> - - <informalexample> - <programlisting role="php"> -<![CDATA[ -<?php - -class SomeClass {} -interface SomeInterface {} -trait SomeTrait {} - -var_dump(new class(10) extends SomeClass implements SomeInterface { - private $num; - - public function __construct($num) - { - $this->num = $num; + public function setLogger(Logger $logger) { + $this->logger = $logger; } +} - use SomeTrait; +$app = new Application; +$app->setLogger(new class implements Logger { + public function log(string $msg) { + echo $msg; + } }); + +var_dump($app->getLogger()); +?> ]]> </programlisting> &example.outputs; <screen> <![CDATA[ -object(class@anonymous)#1 (1) { - ["Command line code0x104c5b612":"class@anonymous":private]=> - int(10) +object(class@anonymous)#2 (0) { } ]]> </screen> </informalexample> <para> - Nesting an anonymous class within another class does not give it access to any - private or protected methods or properties of that outer class. In order to use - the outer class' protected properties or methods, the anonymous class can - extend the outer class. To use the private or protected properties of the outer - class in the anonymous class, they must be passed through its constructor: + Full documentation can be found in the + <link linkend="language.oop5.anonymous">anonymous class reference</link>. </para> - - <informalexample> - <programlisting role="php"> -<![CDATA[ -<?php - -class Outer -{ - private $prop = 1; - protected $prop2 = 2; - - protected function func1() - { - return 3; - } - - public function func2() - { - return new class($this->prop) extends Outer { - private $prop3; - - public function __construct($prop) - { - $this->prop3 = $prop; - } - - public function func3() - { - return $this->prop2 + $this->prop3 + $this->func1(); - } - }; - } -} - -echo (new Outer)->func2()->func3(); -]]> - </programlisting> - &example.outputs; - <screen> -<![CDATA[ -6 -]]> - </screen> - </informalexample> </sect2> + <sect2 xml:id="migration70.new-features.unicode-codepoint-escape-syntax"> <title>Unicode Codepoint Escape Syntax</title> Added: phpdoc/en/trunk/language/oop5/anonymous.xml =================================================================== --- phpdoc/en/trunk/language/oop5/anonymous.xml (rev 0) +++ phpdoc/en/trunk/language/oop5/anonymous.xml 2015-08-09 01:17:17 UTC (rev 337370) @@ -0,0 +1,149 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- $Revision$ --> + +<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook"> + <title>Anonymous classes</title> + + <para> + Support for anonymous classes was added in PHP 7. Anonymous classes are + useful when simple, one-off objects need to be created. + </para> + + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php + +// Pre PHP 7 code +class Logger +{ + public function log($msg) + { + echo $msg; + } +} + +$util->setLogger(new Logger()); + +// PHP 7+ code +$util->setLogger(new class { + public function log($msg) + { + echo $msg; + } +}); +]]> + </programlisting> + </informalexample> + + <para> + They can pass arguments through to their constructors, extend other classes, + implement interfaces, and use traits just like a normal class can: + </para> + + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php + +class SomeClass {} +interface SomeInterface {} +trait SomeTrait {} + +var_dump(new class(10) extends SomeClass implements SomeInterface { + private $num; + + public function __construct($num) + { + $this->num = $num; + } + + use SomeTrait; +}); +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +object(class@anonymous)#1 (1) { + ["Command line code0x104c5b612":"class@anonymous":private]=> + int(10) +} +]]> + </screen> + </informalexample> + + <para> + Nesting an anonymous class within another class does not give it access to + any private or protected methods or properties of that outer class. In order + to use the outer class' protected properties or methods, the anonymous class + can extend the outer class. To use the private or protected properties of + the outer class in the anonymous class, they must be passed through its + constructor: + </para> + + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php + +class Outer +{ + private $prop = 1; + protected $prop2 = 2; + + protected function func1() + { + return 3; + } + + public function func2() + { + return new class($this->prop) extends Outer { + private $prop3; + + public function __construct($prop) + { + $this->prop3 = $prop; + } + + public function func3() + { + return $this->prop2 + $this->prop3 + $this->func1(); + } + }; + } +} + +echo (new Outer)->func2()->func3(); +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +6 +]]> + </screen> + </informalexample> +</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:"~/.phpdoc/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 +--> Modified: phpdoc/en/trunk/language/oop5.xml =================================================================== --- phpdoc/en/trunk/language/oop5.xml 2015-08-09 01:17:12 UTC (rev 337369) +++ phpdoc/en/trunk/language/oop5.xml 2015-08-09 01:17:17 UTC (rev 337370) @@ -41,6 +41,7 @@ &language.oop5.abstract; &language.oop5.interfaces; &language.oop5.traits; + &language.oop5.anonymous; &language.oop5.overloading; &language.oop5.iterations; &language.oop5.magic;

« previous php.doc.cvs (#13553) next »