cvs: phpweb / index.php zend2_example.php zend2_example.phps

From: Date: Wed, 12 Jun 2002 15:25:18 +0000
Subject: cvs: phpweb / index.php zend2_example.php zend2_example.phps
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-10801@lists.php.net to get a copy of this message
imajes Wed Jun 12 11:25:18 2002 EDT Added files: /phpweb zend2_example.phps Removed files: /phpweb zend2_example.php Modified files: /phpweb index.php Log: prevent parse errors. Index: phpweb/index.php diff -u phpweb/index.php:1.254 phpweb/index.php:1.255 --- phpweb/index.php:1.254 Tue Jun 11 12:27:16 2002 +++ phpweb/index.php Wed Jun 12 11:25:18 2002 @@ -126,7 +126,7 @@ handling and provides a much better infrastructure for the integration of external technologies like Java or .NET. For more details and examples you can check out the <a href="ZEND_CHANGES.txt">CHANGES file</a> -and an <a href="http://www.php.net/source.php?url=/zend2_example.php">example script</a> +and an <a href="http://www.php.net/source.php?url=/zend2_example.phps">example script</a> which shows how some of these features can be used.</p> <p><b>Note:</b> This is an alpha version. It should not be used in production Index: phpweb/zend2_example.phps +++ phpweb/zend2_example.phps Example 1: A singleton (static members) ======================================= <?php class Counter { var $counter = 0; function increment_and_print() { print ++$this->counter; print "\n"; } } class SingletonCounter { static $m_instance = NULL; function Instance() { if (self::$m_instance == NULL) { self::$m_instance = new Counter(); } return self::$m_instance; } } SingletonCounter::Instance()->increment_and_print(); SingletonCounter::Instance()->increment_and_print(); SingletonCounter::Instance()->increment_and_print(); ?> Example 2: Factory method (derefencing objects returned from functions) ======================================================================= <?php class Circle { function draw() { print "Circle\n"; } } class Square { function draw() { print "Square\n"; } } function ShapeFactoryMethod($shape) { switch ($shape) { case "Circle": return new Circle(); case "Square": return new Square(); } } ShapeFactoryMethod("Circle")->draw(); ShapeFactoryMethod("Square")->draw(); ?> Example 3: Nested class ======================= <?php class Database { class MySQL { var $host = ""; function db_connect($user) { print "Connecting to MySQL database '$this->host' as $user\n"; } } class Oracle { var $host = "localhost"; function db_connect($user) { print "Connecting to Oracle database '$this->host' as $user\n"; } } } $MySQL_obj = new Database::MySQL(); $MySQL_obj->db_connect("John"); $Oracle_obj = new Database::Oracle(); $Oracle_obj->db_connect("Mark"); unset($MySQL_obj); unset($Oracle_obj); ?> Example 3: Nested class suitable for a PEAR like hierarchy ========================================================== <?php class DB::MySQL { var $host = ""; function db_connect($user) { print "Connecting to MySQL database '$this->host' as $user\n"; } } class DB::Oracle { var $host = "localhost"; function db_connect($user) { print "Connecting to Oracle database '$this->host' as $user\n"; } } $MySQL_obj = new DB::MySQL(); $MySQL_obj->db_connect("Susan"); $Oracle_obj = new DB::Oracle(); $Oracle_obj->db_connect("Barbara"); ?> Example 4: Class constants and class scope ========================================== <?php class ErrorCodes { const FATAL = "Fatal error\n"; const WARNING = "Warning\n"; const INFO = "Informational message\n"; function print_fatal_error_codes() { print "FATAL = " . FATAL; print "self::FATAL = " . self::FATAL; } } /* Call the static function and move into the ErrorCodes scope */ ErrorCodes::print_fatal_error_codes(); ?> Example 5: Regular object method using both local and global functions ====================================================================== <?php class HelloWorld { const HELLO_WORLD = "Hello, World!\n"; function get_hello_world() { return HELLO_WORLD; } function length_of_hello_world() { $str = get_hello_world(); return strlen($str); } } $obj = new HelloWorld(); print "length_of_hello_world() = " . $obj->length_of_hello_world(); print "\n"; ?> Example 6: Multiple derefencing of objects returned from methods ================================================================ <?php class Name { function Name($_name) { $this->name = $_name; } function display() { print $this->name; print "\n"; } } class Person { function Person($_name, $_address) { $this->name = new Name($_name); } function getName() { return $this->name; } } $person = new Person("John", "New York"); print $person->getName()->display(); ?> Example 7: Exception handling ============================= <? class MyException { function MyException($_error) { $this->error = $_error; } function getException() { return $this->error; } } function ThrowException() { throw new MyException("'This is an exception!'"); } try { } catch (MyException $exception) { print "There was an exception: " . $exception->getException(); print "\n"; } try { ThrowException(); } catch (MyException $exception) { print "There was an exception: " . $exception->getException(); print "\n"; } ?> Example 8: __clone() =================== <? class MyCloneable { static $id = 0; function MyCloneable() { $this->id = self::$id++; } function __clone() { $this->name = $clone->name; $this->address = "New York"; $this->id = self::$id++; } } $obj = new MyCloneable(); $obj->name = "Hello"; $obj->address = "Tel-Aviv"; print $obj->id; print "\n"; $obj = $obj->__clone(); print $obj->id; print "\n"; print $obj->name; print "\n"; print $obj->address; print "\n"; ?> Example 9: Unified constructors =============================== <? 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(); ?> Example 10: Destructors ======================= <?php class MyDestructableClass { function __construct() { print "In constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "Destroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?>

« previous php.mirrors (#10801) next »