From: Andi Gutmans Date: Sat, 9 Mar 2002 16:25:51 +0000 (+0000) Subject: - Add the original example script to the CVS so that it's always available. X-Git-Tag: help~115 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=122970574367425e41bc72ee9f09d9aadf546eea;p=php - Add the original example script to the CVS so that it's always available. --- diff --git a/Zend/tests/zend2.php b/Zend/tests/zend2.php new file mode 100644 index 0000000000..f4466b73e2 --- /dev/null +++ b/Zend/tests/zend2.php @@ -0,0 +1,339 @@ +Example 1: A singleton (static members) +======================================= + +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) +======================================================================= + +draw(); + ShapeFactoryMethod("Square")->draw(); + + +?> + +Example 3: Nested class +======================= + +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 +========================================================== + +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 +========================================== + + + +Example 5: Regular object method using both local and global functions +====================================================================== + +length_of_hello_world(); + print "\n"; +?> + +Example 6: Multiple derefencing of objects returned from methods +================================================================ + +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 +============================= + +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() +=================== + +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 +=============================== + + + +Example 10: Destructors +======================= + +name = "MyDestructableClass"; + } + + function __destruct() + { + print "Destroying " . $this->name . "\n"; + } +} + +$obj = new MyDestructableClass(); + +?>