[Connect on page]
PHP 5 / Zend Engine 2.0 improvements
PHP5 allows constants to include expressions, but the expression in compiling time is calculated,
Therefore, constants cannot change its value in operation.
PHP
Class bar {
Const
A = 1 << 0;
Const
B = 1 << 1;
Const
C = a | b;
}
?>
Although the "const" keyword is not defined in the user-defined class or method in the previous code.
But you can run without modification.
Excetions
There is no abnormality in PHP4, and PHP5 introduces an exception handling model similar to other languages.
Phpclass MyExceptionFoo extends Exception {function __construct ($ exception) {parent :: __ construct ($ exception);}} try {throw new MyExceptionFoo ( "Hello");} catch (MyExceptionFoo $ exception) {print $ exception-> getMessage ();}?>
Although the user-defined class or method in the previous code is not defined, 'Catch', 'Throw' and 'Try' Keywords, but there is no need to modify
You can run it.
Function Returns the object value
In PHP4, the function is not possible to return the value of the object and the object returned by the object, with Zend Engine 2
(Zend Engine 2), the following calls are possible:
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 ();
?>
Static member variables in static classes can be initialized
E.g:
phpclass foo {static $ my_static = 5;} print foo :: $ my_static;?>
Static Methods
PHP5 introduces keyword 'static' to define a static method, which can be called from the object.
E.g:
phpclass foo {public static function astaticMethod () {// ...}} foo :: astaticMethod ();?>>
Virtual Variable $ This is invalid in the method defined as a static (static).
INSTANCEOF
PHP5 introduces the "instanceof" keyword to determine if an object is an instance of an object, or a derived of an object, or uses an interface.
Example:
phpclass baseclass {} $ a = new baseclass; if ($ a instanceof basicclass) {echo "hello world";}?> static function variables (static function variables)
All static variables are now processed at compile, which allows developers to specify static variables by reference. This change increases efficiency but means that it is impossible to indirectly quote for static variables.
Parameters passed by reference in the function allow for default values
E.g:
phpfunction my_function (& $ VAR = NULL) {IF ($ VAR === Null) {DIE ("$ VAR Needs to Have a Value");}}?>
__AUTOLOAD ()
When initializing an undefined class, the __ autoload () intercepting function (Interceptor function) will be automatically adjusted
use. The class name will be passed as the __autoload () intercept function. The unique parameter is passed to it.
E.g:
phpfunction __autoload ($ classname) {include_once $ classname. ".php";} $ Object = new classname;?>>
Overloading of methods and attribute calls
All method calls and attribute access can be reloaded universal__call (), __get (), and __set () methods.
Example: __get () and __set ()
phpclass setter {public $ n; public $ x = array ("a" => 1, "b" => 2, "c" => 3); function __get ($ nm) {print "getting [$ nm ] / n "; if ($ this-> x [$ nm])) {$ r = $ this-> x [$ nm]; print" returning: $ r / n "; returnid $ r;} else {Print "Nothing! / N";}}} Function __set ($ NM, $ VAL) {Print "Setting [$ nm] to $ val / n"; if (isset ($ this-> x [$ nm]))) {$ This-> x [$ nm] = $ val; print "ok! / N";} else {print "not ok! / N";}}}} $ foo = new setter (); $ foo-> n = 1; $ foo-> a = 100; $ foo-> a ; $ foo-> z ; var_dump ($ foo);?>
Example: __call ()