* $this
- Unlike in Zend Engine 1 the pseudo variable $this cannot be
- exchanged in Zend Engine 2. You can of course modify or work with
- an object by using $this but you cannot replace $this with another
+ Unlike in Zend Engine 1 the pseudo variable $this cannot be
+ exchanged in Zend Engine 2. You can of course modify or work with
+ an object by using $this but you cannot replace $this with another
object to change the original object.
Example:
$this = $other;
}
}
-
+
$object = new Foo;
$object->prop = 'Hello';
$other = new Foo;
$other->prop = 'Bye';
-
+
$object->replace($other);
-
+
print $object->prop; // still shows 'Hello'
-
+
?>
Zend Engine 2.0 will issue a compile error, if an assignment
* Private and Protected Members.
- The Zend Engine 2.0 introduces private and protected member
- variables. Note that for performance reasons no error message is
- emitted in case of an illegal access to a private or protectecd
+ The Zend Engine 2.0 introduces private and protected member
+ variables. Note that for performance reasons no error message is
+ emitted in case of an illegal access to a private or protectecd
member variable.
Example:
class MyClass2 extends MyClass {
protected $Foo;
-
+
function printHello() {
MyClass::printHello(); /* Should print */
print "MyClass2::printHello() " . $this->Hello; /* Shouldn't print out anything */
print $obj->Foo; /* Shouldn't print out anything */
$obj->printHello();
?>
-
- Protected member variables can be accessed in classes extending the
- class they are declared in, whereas private member variables can
+
+ Protected member variables can be accessed in classes extending the
+ class they are declared in, whereas private member variables can
only be accessed by the class they belong to.
* Private and protected methods.
$o->aPublicMethod();
?>
- Old code that has no user-defined classes or functions named
+ Old code that has no user-defined classes or functions named
'public', 'protected' or 'private' should run without modifications.
* Abstract Classes and Methods.
- The Zend Engine 2.0 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
+ The Zend Engine 2.0 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.
Example:
Classes that do not have abstract methods can be declared abstract
to prevent them from being instantiated.
- Old code that has no user-defined classes or functions named
+ Old code that has no user-defined classes or functions named
'abstract' should run without modifications.
* Interfaces.
}
?>
- Old code that has no user-defined classes or functions named
+ Old code that has no user-defined classes or functions named
'interface' or 'implements' should run without modifications.
-
- An interface may extend one or more base interfaces (but not
+
+ An interface may extend one or more base interfaces (but not
implement them). Neither a class nor an interface can inherit
- methods of the same name from different root interfaces.
-
+ methods of the same name from different root interfaces.
+
Interfaces may contain abstract static methods.
Example:
interface Printable {
function dump();
}
-
+
interface Streamable extends Printable {
function writeToStream();
static function readFromStream();
}
}
?>
-
- A class that does not implement all interface methods must be
+
+ A class that does not implement all interface methods must be
declared as an abstract class.
* Class Type Hints.
- While remaining loosely typed the Zend Engine 2.0 introduces the
- ability to use class type hints to declare the expected class of
+ While remaining loosely typed the Zend Engine 2.0 introduces the
+ ability to use class type hints to declare the expected class of
objects that are passed as parameters to a method.
Example:
* Final methods and classes.
The Zend Engine 2.0 introduces the "final" keyword to declare
- final methods. Those cannot be overridden by sub-classes.
+ final methods. Those cannot be overridden by sub-classes.
Example:
}
?>
- It is furthermore possible to make a class final. Doing this
- prevents a class from being specialized (it cannot be inherited
+ It is furthermore possible to make a class final. Doing this
+ prevents a class from being specialized (it cannot be inherited
by another class). There's no need to declare the methods of
a final class themselves as final.
Properties cannot be final. See per-class constants below.
- Old code that has no user-defined classes or functions named
+ Old code that has no user-defined classes or functions named
'final' should run without modifications.
* Object Cloning.
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
- ensures, that the clone will be initialized with all of the
- properties from the source object, so that developers can start
- with a by-value replica of the source object, and only override
+ ensures, that the clone will be initialized with all of the
+ properties from the source object, so that developers can start
+ with a by-value replica of the source object, and only override
properties that need to be changed.
Example:
print $obj_clone->name . "\n";
print $obj_clone->address . "\n";
?>
-
+
* Unified Constructors.
The Zend Engine allows developers to declare constructor methods
echo 'Foo::constant = ' . Foo::constant . "\n";
?>
- Old code that has no user-defined classes or functions
+ Old code that has no user-defined classes or functions
named 'const' will run without modifications.
* Exceptions.
introduces a exception model similar to that of other programming
languages. But there is no catch all and no finally clause.
- Old code that has no user-defined classes or functions 'catch',
+ Old code that has no user-defined classes or functions 'catch',
'throw' and 'try' will run without modifications.
-
+
Exceptions can be rethrown in catch blocks. Also it is possible to
- have multiple catch blocks. In that case the caught exception is
+ have multiple catch blocks. In that case the caught exception is
compared with the classtype of each catch block from top to bottom
and the first block that has a 'instanceof' match gets executed.
- When the catch block finishes execution continues at the end of
+ When the catch block finishes execution continues at the end of
the last catch block. If no catch block has a 'instanceof' match
- then the next try/catch block is searched until no more try/catch
+ then the next try/catch block is searched until no more try/catch
blocks are available. In that case the exception is an uncaught
exception and the program terminates with showing the exception.
echo $exception;
}
?>
-
+
Even though the above example shows that it is possible to define
exception classes that don't inherit from Exception it is best to
do so. This is because the internal Exception class can gather a
lot of information otherwise not available. The PHP code emulation
code would look something like shown below. The comments show the
- meaning of each property. As the code shows it is possible to read
+ meaning of each property. As the code shows it is possible to read
any available information by using the getter methods. But since
- some of the methods are used internally they are marked final. All
- in all the class is very restrictive because it must be ensured
+ some of the methods are used internally they are marked final. All
+ in all the class is very restrictive because it must be ensured
that anything used internally always works as expected.
-
+
Emulating class Exception:
<?php
$this->trace = debug_backtrace();
$this->string = StringFormat($this);
}
-
- protected $message = 'Unknown exception'; // exception message
+
+ protected $message = 'Unknown exception'; // exception message
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
-
+
private $trace; // backtrace of exception
private $string; // internal only!!
-
+
final function getMessage() {
return $this->message;
}
}
}
?>
-
+
If you derive your exception classes from this Exception base class
- your exceptions will be nicely shown in the builtin handler for
+ your exceptions will be nicely shown in the builtin handler for
uncaught exceptions.
-
- Note: The method getMessage() is a final read only access method to
+
+ Note: The method getMessage() is a final read only access method to
the private proeprty message that is set in the constructor. If you
feel a need to overwrite the exception display then overload method
- __toString() in your derived class or implement your own extneral
+ __toString() in your derived class or implement your own extneral
exception display function to accomplish your desired formatting.
-
+
Example:
-
+
<?php
function display_exception(Exception $ex)
{
echo $ex->getTrace();
echo '</pre>';
}
-
+
try
{
// your code here
}
print foo::$my_static;
-
+
$obj = foo;
-
+
print $obj->my_prop;
?>
* instanceof.
New support for an instanceof operator which checks if an object
is of a certain class or interface type.
-
+
Example:
-
+
<?php
-
+
class Foo {
}
-
+
$obj = new Foo();
if ($obj instanceof Foo) {
print "Yay!\n";
* __autoload().
- The __autoload() interceptor function will be automatically called
- when an undeclared class is to be instantiated. The name of that
- class will be passed to the __autoload() interceptor function as its
+ The __autoload() interceptor function will be automatically called
+ when an undeclared class is to be instantiated. The name of that
+ class will be passed to the __autoload() interceptor function as its
only argument. __autoload() must succeed in loading the class. If it
doesn't then an E_ERROR is emitted.
}
?>
- Each class whose instances can be iterated with foreach should
+ Each class whose instances can be iterated with foreach should
implement the empty interface 'Traversable'. Hence any object
that says it implements 'Traversable' can be used with foreach.
The interfaces 'IteratorAggregate' and 'Iterator' allow to specify
how class objects are iterated in PHP code. The first of them simply
- has a method 'getIterator' which must return an object that either
- implements the interface 'Iterator' or is instantiated from an
+ has a method 'getIterator' which must return an object that either
+ implements the interface 'Iterator' or is instantiated from an
internal class that can be iterated.
Example:
<?php
class ObjectIterator implements Iterator {
-
+
private $obj;
private $num;
-
+
function __construct($obj) {
$this->obj = $obj;
}
$this->num++;
}
}
-
+
class Object implements IteratorAggregate {
-
+
public $max = 3;
-
+
function getIterator() {
return new ObjectIterator($this);
}
- }
-
+ }
+
$obj = new Object;
-
+
// this foreach ...
foreach($obj as $key => $val) {
echo "$key = $val\n";
}
-
+
// matches the following 7 lines with the for directive.
$it = $obj->getIterator();
for($it->rewind(); $it->valid(); $it->next()) {
unset($it);
?>
- The matching for directive is very intersting here since it shows
+ The matching for directive is very intersting here since it shows
the use of all abstract methods declared in the interfaces Iterator
and IteratorAggregate respectively.
* __METHOD__
- The pseudo constant __METHOD__ shows the current class and method
- when used inside a method and the function when used outside of a
+ The pseudo constant __METHOD__ shows the current class and method
+ when used inside a method and the function when used outside of a
class.
Example:
* __toString()
- The magic method __toString() allows to overload the object to
- string conversion. This conversion is only done automatically for
- the printing functions (echo, print) but not for other functions
+ The magic method __toString() allows to overload the object to
+ string conversion. This conversion is only done automatically for
+ the printing functions (echo, print) but not for other functions
that expect strings. Also the function __toString is not used in
places where objects are not allowed but strings are like array
indices. Note that specialized objects may be converted to a string
?>
* Reflection API
-
- PHP5 comes with a complete reflection API that adds the ability to
+
+ PHP 5 comes with a complete Reflection API that adds the ability to
reverse-engineer classes, interfaces, functions and methods as well
as extensions.
-
- The reflection API also offers ways of getting doc comments for
+
+ The Reflection API also offers ways of getting doc comments for
functions, classes and methods.
-
+
Nearly all aspects of object oriented code can be reflected by
- using the reflection API which is documented separatley:
+ using the Reflection API which is documented separately:
http://sitten-polizei.de/php/reflection_api/docs/language.reflection.html
Example:
}
}
- reflection_class::export('Foo');
- reflection_object::export(new Foo);
- reflection_method::export('Foo', 'func');
- reflection_property::export('Foo', 'prop');
- reflection_extension::export('standard');
+ ReflectionClass::export('Foo');
+ ReflectionObject::export(new Foo);
+ ReflectionMethod::export('Foo', 'func');
+ ReflectionProperty::export('Foo', 'prop');
+ ReflectionExtension::export('standard');
?>
* New memory manager
* Others
Probably other changes which we forgot to list. This list will be kept up-to-date
as much as possible.
-
+
Changes in the Zend Engine 1.0