<?php
class MyClass {
- private $Hello = "Hello, World!\n";
+ private $Hello = "Hello, World!\n";
- function printHello() {
- print $this->Hello;
- }
+ function printHello() {
+ print $this->Hello;
+ }
}
class MyClass2 extends MyClass {
- function printHello() {
- MyClass::printHello(); /* Should print */
- print $this->Hello; /* Shouldn't print out anything */
- }
+ function printHello() {
+ MyClass::printHello(); /* Should print */
+ print $this->Hello; /* Shouldn't print out anything */
+ }
}
$obj = new MyClass();
<?php
class MyCloneable {
- static $id = 0;
+ static $id = 0;
- function MyCloneable() {
- $this->id = self::$id++;
- }
+ function MyCloneable() {
+ $this->id = self::$id++;
+ }
- function __clone() {
- $this->name = $clone->name;
- $this->address = "New York";
- $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";
+ $obj->name = 'Hello';
+ $obj->address = 'Tel-Aviv';
print $obj->id . "\n";
<?php
class DB::MySQL {
- var $host = "";
+ var $host = '';
- function db_connect($user) {
- print "Connecting to MySQL database '$this->host' as $user\n";
- }
+ function db_connect($user) {
+ print "Connecting to MySQL database '$this->host' as $user\n";
+ }
}
class DB::Oracle {
- var $host = "localhost";
+ var $host = 'localhost';
- function db_connect($user) {
- print "Connecting to Oracle database '$this->host' as $user\n";
- }
+ function db_connect($user) {
+ print "Connecting to Oracle database '$this->host' as $user\n";
+ }
}
$MySQL_obj = new DB::MySQL();
- $MySQL_obj->db_connect("Susan");
+ $MySQL_obj->db_connect('Susan');
$Oracle_obj = new DB::Oracle();
- $Oracle_obj->db_connect("Barbara");
+ $Oracle_obj->db_connect('Barbara');
?>
* Classes may contain constants.
Example:
<?php
- class foo {
- const hey = "hello";
- }
+ class foo {
+ const hey = 'hello';
+ }
- print foo::hey;
+ print foo::hey;
?>
* Current namespace's symbol tables are searched first for
the same name.
<?php
- define("foo", "bar");
+ define('foo', 'bar');
- class FooClass {
- const foo = "foobar";
+ class FooClass {
+ const foo = 'foobar';
function printFoo() {
- print foo;
+ print foo;
}
- }
+ }
?>
* In the scope of a function, the current namespace is that
Example:
<?php
- class FooClass {
+ class FooClass {
function foo() {
- $this->bar();
- bar();
+ $this->bar();
+ bar();
}
function bar() {
- print "foobar\n";
+ print "foobar\n";
}
- }
+ }
- $obj = new FooClass;
- $obj->foo();
- $obj->foo();
+ $obj = new FooClass;
+ $obj->foo();
+ $obj->foo();
?>
This prints "foobar" two times, since a bar() method exists
<?php
class MyClass {
- function hello() {
- print "Hello, World\n";
- }
+ class MyClass2 {
+ function hello() {
+ print "Hello, World in MyClass2\n";
+ }
+ }
- class MyClass2 {
function hello() {
- print "Hello, World in MyClass2\n";
+ print "Hello, World\n";
}
- }
}
import function hello, class MyClass2 from MyClass;
<?php
class MyOuterClass {
- class MyInnerClass {
- function func1() {
- print "func1()\n";
+ class MyInnerClass {
+ function func1() {
+ print "func1()\n";
+ }
+
+ function func2() {
+ print "func2()\n";
+ }
}
-
- function func2() {
- print "func2()\n";
- }
- }
}
import class * from MyOuterClass;
<?php
class MyOuterClass {
- const Hello = "Hello, World\n";
+ const Hello = "Hello, World\n";
}
import const Hello from MyOuterClass;
<?php
class BaseClass {
- function __construct() {
- print "In BaseClass constructor\n";
- }
+ function __construct() {
+ print "In BaseClass constructor\n";
+ }
}
class SubClass extends BaseClass {
- function __construct() {
- parent::__construct();
- print "In SubClass constructor\n";
- }
+ function __construct() {
+ parent::__construct();
+ print "In SubClass constructor\n";
+ }
}
$obj = new BaseClass();
Example:
<?php
- class MyDestructableClass {
+ class MyDestructableClass {
function __construct() {
- print "In constructor\n";
- $this->name = "MyDestructableClass";
+ print "In constructor\n";
+ $this->name = 'MyDestructableClass';
}
function __destruct() {
- print "Destroying " . $this->name . "\n";
+ print 'Destroying ' . $this->name . "\n";
}
- }
+ }
- $obj = new MyDestructableClass();
+ $obj = new MyDestructableClass();
?>
Like constructors, parent destructors will not be called
<?php
class MyException {
- function __construct($exception) {
- $this->exception = $exception;
- }
+ function __construct($exception) {
+ $this->exception = $exception;
+ }
- function Display() {
- print "MyException: $this->exception\n";
- }
+ function Display() {
+ print "MyException: $this->exception\n";
+ }
}
class MyExceptionFoo extends MyException {
- function __construct($exception) {
- $this->exception = $exception;
- }
+ function __construct($exception) {
+ $this->exception = $exception;
+ }
- function Display() {
- print "MyException: $this->exception\n";
- }
+ function Display() {
+ print "MyException: $this->exception\n";
+ }
}
try {
- throw new MyExceptionFoo("Hello");
+ throw new MyExceptionFoo('Hello');
}
catch (MyException $exception) {
- $exception->Display();
+ $exception->Display();
}
?>
Example:
- <?php
- class Circle {
- function draw() {
- print "Circle\n";
+ <?php
+ class Circle {
+ function draw() {
+ print "Circle\n";
+ }
}
- }
- class Square {
- function draw() {
- print "Square\n";
+ class Square {
+ function draw() {
+ print "Square\n";
+ }
}
- }
- function ShapeFactoryMethod($shape) {
- switch ($shape) {
- case "Circle": return new Circle();
- case "Square": return new Square();
+ function ShapeFactoryMethod($shape) {
+ switch ($shape) {
+ case 'Circle': return new Circle();
+ case 'Square': return new Square();
+ }
}
- }
- ShapeFactoryMethod("Circle")->draw();
- ShapeFactoryMethod("Square")->draw();
- ?>
+ ShapeFactoryMethod('Circle')->draw();
+ ShapeFactoryMethod('Square')->draw();
+ ?>
* Static member variables of static classes can now be
initialized.
Example:
<?php
- class foo {
- static $my_static = 5;
- }
+ class foo {
+ static $my_static = 5;
+ }
- print foo::$my_static;
+ print foo::$my_static;
?>
* Supporting default values for by-reference function parameters.
Example:
<?php
- function my_function(&$var = null) {
- if ($var === null) {
- die('$var needs to have a value');
- }
- }
+ function my_function(&$var = null) {
+ if ($var === null) {
+ die('$var needs to have a value');
+ }
+ }
?>
without further coding. The return value may be changed by
returning a value from the global scope of the included file or
the evaluated string. For example, if 'return 7;' is executed in
- the global scope of foo.inc, include("foo.inc") would evaluate
+ the global scope of foo.inc, include('foo.inc') would evaluate
to 7.
* Automatic resource deallocation.
For example:
- $foo[5]["bar"] = "foobar";
+ $foo[5]['bar'] = 'foobar';
print "{$foo[5]["bar"]}"; // would print "foobar"
* Ability to call member functions of other classes from within