(1) The basic literary method used by Zend 2.0 is now called a script compilation engine called Zend Engine. This is one of the reasons for the excellent function of PHP4, which is a language generated for the improvement of PHP3. Everyone has always believed that the performance of PHP4 is based on the original goal, which has greatly enhanced than PHP3, accounting for a lot in the world of network programming.
Zend, developed Zend Engine, is incorporated by the company that is developed by PHP3, the company's major developers of PHP3. The name of Zend is composed of Zeev and Andi's name. Zend's business model is, continuously providing the Open Source's PHP core (Core), while enhancing the benefits of surrounding products and trafficking. As the foundation of Open Source Software as a base plate, most of the world's business is a better typical example.
■ Limitation of PHP4
The blessing of the success of PHP4, this application is gradually widened. As an enterprise-based use, it is smelling when using PHP. Therefore, there is such a problem, when building a large-scale website, the reuse of the code is very poor. Specifically, the object-oriented performance of PHP4 is very weak, so it is accustomed to using Java and other technicians to complain about this.
Gradually improve the object-oriented performance of PHP4, a large change of basic literary law, and developers reached the development purpose of updating the PHP description method.
■ Zend 2.0 began development Subsequently, the developers of Zend PHP Center issued the concept of Zend 2.0 engine for the next generation of PHP language engines in July 2001. While [Zend Engine Version 2.0: Feature Overview and design], the object-oriented performance is greatly enhanced while the object-oriented performance is greatly enhanced. The expansion of the current PHP4 Zend engine is exactly the same as the past PHP3. This means that it is necessary to enhance the main version number of the new language engine, clear method goals, and meet the praise from the development group.
Ze2 development, like the previous Zend Engine, is running in Open Source mode. The latest source code is fully open on CVS because it is an open developer, and the discussion on development is very active.
Now Ze2 is determined in the next version PHP5 in PHP. The final release time is still not fixed, but if the newsletter released on April 1, 2003, it should be Beta Release.
(2) New features of PHP5
Next, please take a look at the performance of the powerful PHP5. The first is the most important object-oriented performance, and the entity characteristics of the class are greatly modified. It is only about the new features of the class.
· The reference transition of the object is the default (defraction) to introduce access attributes • Restrictions on access methods · Abstract classes and abstract methods · Interface · Final Declaration · Name Space · Class Constant · Class Variables · Unified Builder · Analysis Structure (Distructor) · Other accessory features
The above is written in accordance with the login version of the data on CVS on April 22, 2003, there is also a variation possibility before formal release.
■ The default reference transition of the object
In PHP4, when the variable $ var1 is an entity object, if $ var2 = $ var1; then, in $ VAR2, the replication of $ VAR1 is substituted. Obviously, $ VAR2 To point to the same object as $ VAR1, we must write $ VAR2 = & $ VAR1, you must add & as a reference. In PHP5, the substitution of the object will become an automatic reference transition. That is to say, $ var2 = $ var1, both point to the same object. If you want to use PHP4, bring your COPY, then use the method of importing __clone (). $ VAR2 = $ VAR1 -> __ clone (); here, Clone is two consecutive "_" (this is just the characteristics of the entity) ■ Introducing the restriction of access attributes
In the class of PHP4, along with attributes and methods, there is no limit to anywhere in the inside and outside of the class. Therefore, the user cannot prevent the inadvertent changes of the attribute.
In PHP5, like C and Java, the access restrictions of the three levels of private, protected, and public, enable the designer of the class to limit the use of the properties and methods. The following is the meaning of various access limits.
PUBLIC: You can use anywhere in the inside and outside of the class, change · private: can only be used in this class method, change · protected: can be in this class and inherited another class of this class. Reference, change. In addition, in the inherited class, access designation can be written.
"VAR" in PHP4, the same meaning as PUBLIC is the same as in the past. Let's take an example, let's take a look at how the access restrictions work.
PHP code: ----------------------------------------------- -------------------------------- Class Hoge1 {Private $ VAR1 = 'A'; protected $ var2 = 'b '; Protected $ var3 =' c ';
Function setLower () {$ this-> var1 = 'a'; $ this-> var2 = 'b'; $ this-> var3 = 'c';} function var1 () {return1-> var1;} VAR2 () {Return $ this-> var2;} function var3 () {return $ this-> var3;}}
-------------------------------------------------- ------------------------------
In this class, there are three attributes with $ VAR1, $ VAR2, $ VAR3. $ VAR1 is declared as Private, $ VAR2 and $ VAR3 are protected. here
PHP code: ----------------------------------------------- ------------------------------- $ HoGE = New Hoge1; Echo'var1: '. $ HoGE-> var1 "N"
-------------------------------------------------- ------------------------------
If an attempt is made not allowed to access the private property from the outside, then the following error occurs:
Fatal Error: Cannot Access Private Property Hoge1: $ VAR1 IN /PATH/to/Script.php Online XX is also the same for Protected $ VAR2.
However, because the way $ HoGE is not private and protected, the following code can work properly, returns the value of the internal private and protective variables.
PHP code: ----------------------------------------------- -------------------------------- Echo 'var1:'. $ hoge-> var1 (). "/ n "; // var1: aecho 'var2:'. $ Hoge-> var2 ()." / N "; // var2: becho 'var3:'. $ Hoge-> var3 ()." / N "; // VAR3: C
$ hoge-> setlower ();
Echo 'Var1:'. $ HoGE-> var1 (). "/ n"; // var1: aecho 'var2:'. $ hoge-> var2 (). "/ n"; // var2: becho 'var3: '. $ hoge-> var3 (). "/ n"; // var3: c
-------------------------------------------------- ------------------------------
Second, in order to see the status of Protected properties, we tried to create a class HoGe2 inherited Hoge1.
PHP code: ----------------------------------------------- --------------------------------- Class Hoge2 Extends Hoge1 {Public $ VAR3 = '3';
Function D_VAR1 () {RETURN $ this-> var1;} function d_var2 () {RETURN $ this-> var2;} function d_var3 () {Return $ this-> var3;}}
-------------------------------------------------- ------------------------------
In class HoGe2, only $ VAR3 is declared as public. In the case where the attribute is protected, what is the restriction from a subclass to the subclass, which is determined by the attribute declaration of the subclass. In HoGe2, because $ VAR3 is declared as public, there is no matter where you can access $ VAR3 (entity is $ var3). Because $ VAR1 is private in HoGe1, the $ VAR1 in HoGe1 in the HoGe2 subclass will not be inherited, and it is possible to make a property name $ VAR1 in HoGe2, so you must clearly distinguish between HOGE1 :: $ VAR1 and HOGE2 :: $ VAR1.
PHP code: ----------------------------------------------- -------------------------------- $ HoGE = New Hoge2;
Echo 'Var1:'. $ HoGE-> VAR1. "/ n"; // var1: // echo 'var2:'. $ hoge-> var2. "/ n"; // errorecho 'var 3:'. $ hoge -> var3. "/ n"; // var3: 3echo 'var1:'. $ hoge-> d_var1 (). "/ n"; // var1: echo 'var2:'. $ hoge-> d_var2 (). "/ n"; // var2: becho 'var 3:'. $ hOGE-> D_VAR3 (). "/ n"; // var3: 3
-------------------------------------------------- ------------------------------
$ HoGE-> var1 is a variable that doesn't matter with HoGe1 :: var1, so there is no display because VAR2 has protected accessed restrictions, so if you don't pass Method, you will have a fatal error.
■ Introducing a limit to access methods
As in the above, there are three types into private, protected, public.
Public: Call from anywhere · Private: You can only call from this class Method · protected: only can call from this class and subclass's Method
Here is the same as Java and C , please don't mix.
■ Abstract (Abstract) class and abstract method
Supports the same abstraction and abstract method as Java. The abstract method only provides a way of calling modes, and does not provide entities. In addition, the class of abstract methods must be abstracted. If you want to directly create an abstract class object, then a fatal error will appear.
Fatal Error: Cannot Instantiate Abstract Class ClassName
The actual example of generating errors is as follows:
PHP code: ----------------------------------------------- ---------------------------------
Abstract class myabstract {Abstract public function test (); public function test2 () {echo "myabstract :: test2 () caled./n";}}
Class MyImplement Extends MyAbstract {public function test () {echo "MyImplement :: test () caled./n";}}
$ obj = new myimplement; $ obj-> test ();
?>
-------------------------------------------------- ------------------------------
■ Interface (Interface)
Supports the same interface (interface) as Java. The interface is a combination of external call forms that are suitable for the described. The entity of the interface cannot be recorded. Conversely, the class that implements the interface must hold an entity corresponding to the method of this interface. In addition, the class can implement multiple interfaces, so it is possible to achieve multiple inheritance.
PHP code: ----------------------------------------------- --------------------------------- Interface throwable {public function getMessage ();
Interface serializable {public function toString ();
Class MyException Implements Throwable, Serializable {Public Function GetMessage () {Return 'this is myexception message';}
Public function toString () {return 'myexception: this is myexception message';}}
$ E = New MyException; Echo $ E-> getMessage (); Echo $ E-> TOSTRING ();?>
-------------------------------------------------- ------------------------------
■ Final statement
Like Java, PHP5 supports Final statement. This method will definitely be overridden in a subclass for a method to append Final statement. If the method is declared by Final, it is still overloaded in the subclass, and there is a mistake:
PHP code: ----------------------------------------------- --------------------------------- Fatal Error: Cannot Override Final Method Fuga :: foo ()
-------------------------------------------------- ------------------------------
An example of errors:
PHP code: ----------------------------------------------- ---------------------------------
Class Fuga {FINAL FUNCTION FOO () {Echo "this is final function / n";}}
Class Hoge Extends Fuga {Function Foo () {Echo "This Is Not Final Function / N";}}?>
-------------------------------------------------- ------------------------------
(3) New features of PHP5 (continued)
PHP5 release plan
In the previous article, "According to Zend, the message should be Beta Release according to the message released on April 1, 2003, but the result of the internal discussion of the developer is that Beta is fashionable, and it is possible Not Beta Release.
For this purpose, you can refer to News: //news.php.net/ PHP.Version5.dev:372
In this document, the PHP5 release plan returned to a white paper, and on the other hand, the development of Zend Engine2 is in progress. PHP5's release is actually a general thing to look forward to "hurry to go to the year".
New features of PHP5
Then let's take a look at the additional new functions of the class.
■ name space
PHP5 supports the namespace. Therefore, we can load classes, variables, constants, and functions in the namespace. In the PHP4 scope, only the three types of the global, functions, and within the class, so pay special attention to "pollution" Global space will be easily "not paying attention. If you use a namespace, we can separate the variable namespace in Package, so you should easily make independent packages.
Examples are as follows:
PHP code: ----------------------------------------------- --------------------------------- Namespace this {class hoge {} constant = 'this constant'; Function Afunction () {} Var $ avariable = 'this variable';}
$ obj = new this :: Hoge; Echo this :: aconstant. "/ n"; this :: Afunction (); echo this :: $ avariable. "/ n";
-------------------------------------------------- ------------------------------
Suppose you should do this if you want to access objects in the namespace:
Name :: Object Name
However, the name of PHP5 does not have a difference between C .
■ Class inside the constant
Use the keyword const, you can define constants in the class, namespace. Because it is constant, it must be added to the front of the constant name. Constants within Class, higher than the Global constant in this class.
Here Const is an appoken language, so it is necessary to make the necessary corrections when using const in the Class name and function name.
PHP code: ----------------------------------------------- ---------------------------------
Define ('Constant_Value', 'Global Constant');
Class myclass {const constant_value = 'class constant';
Function PrintConstant () {print constant_value;}}
Echo myclass :: constant_value. "/ n"; myclass: rintConstant ();?>>
-------------------------------------------------- ------------------------------
In this example, myclass: rintConstant () is a value that displays constant constant_value, but Constant_Value exists in both places in the Global space and Class. In this case, the constant level of constant constant_value in myclass is high, and is displayed as "Class Constant".
■ Object variable
Even if the class is not instantiated, the object variable can be initialized according to the specified value. The way to access is as follows:
Classification: $ Variable Name
PHP code: ----------------------------------------------- ---------------------------------
Class Hoge {Static $ my_static = 5;}
Print Hoge: $ my_static;?> --------------------------------------- ---------------------------------------
■ Unified Builder
When generating an object, the method capable of automatically calling is referred to as "builder."
The builder in PHP4 is the same method name as the Class name. This is the same place as Java and C , so there will be no sleep for some people who are used. However, if you want to call the builder of the parent class from the subclass, you must also write the name of the parent class in PHP.
In PHP, the builder of the parent class cannot be called automatically, so the case is more. In PHP5, unified use of the __constructor builder name, regardless of the Class name, all known as __construct (), is treated as a builder.
In addition, considering the interchangeability of PHP4, if there is a previous builder name that exists in the Class name, then the builder is prioritized.
PHP code: ----------------------------------------------- ---------------------------------
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 ();?>>
-------------------------------------------------- ------------------------------
■ Destructor
In contrast to the builder, the method automatically called when the object is released, is referred to as a destructor.
The PHP4 supports the destructive function, and only the function called by register_shutdown_function () when logging in to the PHP run termination, only similar implementation methods. The PHP5 formally supports the destructor and can specify the action when the object is released in the class.
The destructor is a method called __destruct. When the reference counter inside the object becomes 0, __ designRuct () is called, and the memory used by the object is released.
PHP code: ----------------------------------------------- ---------------------------------
Class MyDestructableClass {function __construct () {print "in constructor / n"; $ this-> name = 'mydestructableclass';}
Function __destruct () {print 'destroying'. $ this-> name. "/ n";}}
$ obj = new mydestructableclass ();?>
-------------------------------------------------- ------------------------------
In addition, the same place as the builder is that the destructor of the parent class cannot be called automatically. When necessary, you need to use the command: Parent :: __ destruct ();
■ Access
In PHP4, if you have accessed an existing property, the system will automatically generate new properties corresponding to it.
PHP code: ----------------------------------------------- -------------------------------- Class Hoge {}
$ obj = new hoge; $ obj-> prop = "this is new property";
-------------------------------------------------- ------------------------------
As shown above, when the value is reducing a value that does not exist, the sub-entry point will automatically generate a new attribute. Similarly, accessing a non-existent attribute is like a variable that is grouped into a NULL value, does not have errors.
Append a little in PHP5 is the ability to control any attributes. In the class, if there is a method of __set (), __ get (), alternatively, the method herein will be called. E.g:
PHP code: ----------------------------------------------- ---------------------------------
Class Hoge {Function __set ($ Name, $ Value) {Print "__set () IS Called with ($ Name, $ Value) / N"; $ this -> $ name = value;}}
$ obj = new Hoge;
$ OBJ-> A = '123'; $ OBJ-> A = '456'; $ OBJ-> B = '789';?>
-------------------------------------------------- ------------------------------
Here, the __ set method is used as a substitute method that is not defined attributes, and the value is re-defined after the value is after the display value.
PHP code: ----------------------------------------------- -------------------------------- $ OBJ-> A = '123';
-------------------------------------------------- ------------------------------
When this sentence is executed, because there is no property a at this time, it is replaced, and the __ set method is called.
__set () IS Called with (a, 123)
Secondly,
$ OBJ-> A = '456';
Once again, $ OBJ-> A, this time, because the attribute A already exists, the __set is not called, and the value will be reached into the attribute A as usual.
$ OBJ-> B = '789';
This time, we take the value into another attribute B, just like a first case of A.
__set () IS Called with (B, 789)
In contrast to the __set method, __ get method is called when referenced to the presentation of the attribute that does not exist. Combine these two, then look at the access to attributes, in fact, using it can write different responses in different occasions. PHP code: ----------------------------------------------- ---------------------------------
Class Hoge {Public $ Properties;
Function __set ($ name, $ value) {$ this-> Properties [$ name] = $ value;} function __get ($ name) {return $ this-> Properties [$ name];}}
$ obj = new Hoge;
$ OBJ-> A = '123'; $ OBJ-> B = '456'; Echo $ OBJ-> A; Echo $ OBJ-> B;
Print_r ($ OBJ);?>
-------------------------------------------------- ------------------------------
In this example, access to all attributes in the class is loaded in $ Properties so that the properties we join are not directly attached to the object. This is an example that is not easy to understand, for example, try saving to $ Properties in this example into a file or a database. In fact, in the object, we can make a simple implementation of many complex operations.
Some of the __set, __get, but __call can also be used to write a method of absence, when we call the object to the object as the following example,
$ Object-> methodname ();
If there is no methodname in this class, it is usually the following error:
Fatal error: Call to undefined method class :: methodname ()
However, if this class exists __call method, as an alternative, __ call is called. The parameters of __call have two, the first parameter is called method name, and the second parameter is an array that holds the called parameter. Other methods can also be used in addition to the following examples.
PHP code: ----------------------------------------------- ---------------------------------
Class Proxy {Private $ Object;
Function __call ($ name, $ params) {if ($ this-> object)) {if ($ this-> Object, $ name)) {RETURN CALL_USER_FUNC_ARRAY (Array ($ this-> Object, $ name) ), $ params);} el exists ";}}} function __construct ($ Object) {$ this-> object = $ object;}}
Class Hoge {Function Add ($ VAR1, $ VAR2) {RETURN $ VAR1 $ VAR2;}}
$ P = New Proxy (New Hoge);
$ Result = $ P-> Add (1, 2); Echo "Result: $ Result / N"; $ Result = $ P-> Sub (5, 3); Echo "Result: $ results / N";?> -------------------------------------------------- ----------------------------- The object processing part of the new object model PHP has been completely rewritten, with better performance And more features. In previous versions of PHP, the object is the same as the built-in variable type (such as Integer and String) processing method, which is the object replica when the variable is assigned an object or object as a parameter transmission. In the new version, the object is referenced by the handle, not through its value. (Handle can be considered an identifier of the object) Many PHP programmers may not be aware of the "copy quirks" of the previous object model, so the previous PHP program will not need to do any changes, or only small changes You can run private and protected members PHP 5 introduced private and protected member variables, which can define when the class properties can be accessed. The protected member variable of the Class Class can be accessed in the extended class of this class, while private member variables can only be accessed in this class. Class myclass {private $ hello = "hello, world! / n"; protected $ bar = "Hello, foo! / n"; protected $ foo = "Hello, bar! / n";
Function printhello () {print "Myclass :: PrintHello ()". $ this-> hello; print "Myclass :: PrintHello ()". $ this-> bar; print "myclass :: printhello ()". $ this- > Foo;}}
Class myclass2 extends myclass {protected $ foo;
Function printhello () {Myclass :: Printhello (); / * shouth price * / print "myclass2 :: printhello ()". $ this-> hello; / * Shouldn't print out anything * / print "Myclass2 :: Printhello () "$ this-> bar; / * Shouldn't Print (not Declared) * / Print" Myclass2 :: PrintHello () ". $ this-> foo; / * Should Print * /}}
$ obj = new myclass (); print $ obj-> hello; / * shouldn't print out anything * / print $ obj-> bar; / * shouth't print out anything * / print $ obj-> foo; / * Shouldn't Print Out Anything * / $ OBJ-> PrintHello (); / * Should Print * /
$ obj = new myclass2 (); print $ obj-> hello; / * shouldn't print out anything * / print $ obj-> bar; / * shouth't print out anything * / print $ obj-> foo; / * Shouldn't Print Out Anything * / $ OBJ-> PrintHello ();?> ------------------------------- -------------------------------------------------- Private and protection methods In PHP 5 (Zend Engine 2), private and protective methods were introduced. Example: Class foo {private function aprivateMethod () {echo "foo :: AprivateMethod () called./n"; }protected function aprotected () caled () {echo" foo :: Aprotected); AprivateMethod ();}}
Class Bar Extends Foo {Public Function ApublicMethod () {Echo "Bar :: ApublicMethod () Called./n"; $ this-> AprotacectedMethod ();}}
$ o = new bar; $ O-> ApublicMethod ();?> Although "Public," Protected "or" Private "or the keywords such as" PUBLIC, "Protected" or "Private" in previous codes are not defined, but you don't need to edit run. Abstract classes and methods PHP 5 also introduced abstractions and methods. Abstract methods only declare the method definition, not for actual operation. Classes containing abstract methods need to be declared as abstract classes. Example: Abstract Class AbstractClass {Abstract Public Function Test ();
Class Implement Class Extends AbstractClass {Public Function Test () {Echo "ImplementedClass :: test () called./n";}
$ o = new importededclass; $ o-> test ();?> Abstract class cannot be instantiated. Although "Abstract" keywords or methods in previous codes are not defined, but you can run without editing. Interface Zend Engine 2.0 introduces an interface. A class can run any interface list. Example: Interface throwable {public function getMessage ();}
Class Exception Implements Throwable {public function getMessage () {// ...}?> The "interface" keyword in the previous code is not defined, but you can run without editing. Class Type Definition In the reserved class without the need to define type, PHP 5 introduces class type definition to declare which class is passed to a method through parameters. Example: Interface foo {function a (foo $ foo);}
Interface bar {Function B (bar $ bar);
Class Foobar Implements Foo, Bar {Function A (Foo $ FOO) {// ...}
Function B (Bar $ bar) {// ...}}
$ a = new foobar; $ b = new foobar; $ A-> A ($ b); $ A-> b);?> These types definitions are not like some types of predefined languages in compilation Introduction is performed, but during runtime. This means: Function Foo (ClassName $ Object) {// ...}?> Isometric: Function Foo ($ Object) {IF ($ Object InstanceOf classname) {Die ("argument 1 must be an ");}}?> This method is only used for object or class, which is not applicable to built-in types. -------------------------------------------------- ------------------------------- Finalphp 5 Introduced "Final" keyword definition member that cannot be overwritten in subclass Or method. Example: Class Foo {Final Function Bar () {// ...}}?> Although "Final" keywords in previous code are not defined, but you can run without editing. Object clone PHP 4 When the object is copied, the user cannot determine the mechanism of the copy. At a replication, a PHP 4 is replicated with an exproportion of the original object. We don't have a fully equipped replica every time. A good need for a replication mechanism is that when there is an object representing a GTK window, it has all the resources of the window. When you create a copy, you may need a new window, which has the original window All attributes, but you need to have a resource of a new window. Another example is that you have an object to reference another object. When you copy the parent object, you want to build a new instance of the reference object to enable the replica to reference it. For a copy of an object through calling the __clone () method: $ copy_of_object = Object -> __clone ();?> When the developer requests to create a new copy of an object, the Zend engine checks if it is defined. _clone () method. If it is not defined, it calls a default __clone () method to copy all the properties of the object. If this method is defined, the method is responsible for setting the necessary properties in the copy. For convenience, the engine provides a function to import all properties from the source object so that it can get a copy of the source object with a value, only you need to overwrite the properties that you need to change. Example: Class mycloneable {static $ ID = 0;
Function mycloneable () {$ this-> id = Self :: $ ID ;}
Function __clone () {$ this-> name = $ THAT-> Name; $ this-> address = "new york"; $ this-> id = self :: $ ID ;}}
$ obj = new mycloneable ();
$ obj-> name = "hello"; $ obj-> address = "tel-aviv";
Print $ OBJ-> ID. "/ n";
$ Obj = $ OBJ -> __ clone ();
Print $ OBJ-> ID. "/ n"; Print $ Obj-> name. "/ n"; Print $ OBJ-> Address. "/ n";> Unified Construction Method Name The Zend Engine allows developers to define classes Construction method. The class with a structure of a construction method first calls the construction method, and the construction method is applicable to initialization pre-use before the class. In PHP4, the name of the constructor is the same as the class name. Since the process of calling the parent class is generally common in the derived class, it is a bit awkward when moving in a large class inheritance in PHP4. When a derived class is moved into a different parent class, the constructor name of the parent class is inevitably different. In such words, the statement in the derived class is required to be rewritten. PHP 5 Introduces A Standard Way of Decilaning Constructor Methods By Calling Them By The name __construct (). PHP5 introduces method name __construct () to define constructors. Exampleclass 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 ();?> To look down, PHP5 is a class name to find the constructor when the __construct () method is not found in the class. This means that the only compatibility problem may have used a method name called __construct () in the previous code. -------------------------------------------------- ----------------------------- sect method method defines a destructive method. The destructure method can record debugging information, turn off database connections, and other sweeping work. There is no such mechanism in PHP4, although PHP has supported registration to run function when the request is completed. PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as Java: When the last reference to an object is destroyed the object's destructor, which is a class method name% __ destruct ()% that recieves no parameters, is Called Before The Object Is Freed from Memory. PHP5 introduces similar methods similar to other object-oriented languages such as Java languages: When the reference is removed by the last object, the system will release before the object is released from memory. Destructure method for __destruct (). Example: class mydestructableclass {function __construct () {print "in constructor / n"; $ this-> name = "mydestructableclass";
Function __destruct () {Print "destroying". $ this-> name. "/ n";}}
$ obj = new mydestructableclass ();?> and constructor are similar, the engine will not call the destructual method of the parent class, to call the method, you need to pass the Parent :: _ DESTRUCT () statement in the destructure method of the subclass. transfer. Constant PHP 5 introduces a common definition: class foo {constant = "constant";} echo "foo :: constant =". Foo :: constant. "/ N";?>
PHP5 allows expressions in constants, but the expression in the compile time will be calculated. Therefore, constants cannot change its value in operation. Class bar = 1 << 0; const b = 1 << 1; const c = a | b;}?> Although "const" keywords in user custom classes or methods in previous codes are not defined, You can run without editing. Exception PHP 4 Had No Exception Handling. PHP 5 Introduces A Exception Model Similar To That of Other Programming Languages. There is no exception processing in PHP4, and PHP5 references the exception processing model similar to other languages. Example: class myexceptionfoo extends exception {function __construct ($ exception) {parent :: __ construct ($ exception);}}
Try {throw new myexceptionfoo ("Hello");} catch (myException $ exception) {print $ exception-> getMessage ();}?> Although the user-defined class or method in the previous code is not defined 'catch', ' Throw 'and' Try 'keywords, but you can run without editing. The function returns the value of the object In PHP 4 it was not possible to dereference objects returned by functions and make further method calls on those objects With the advent of Zend Engine 2, the following is now possible:. In PHP4, the function returns the object can not The value is called by the returned object, through the Zend Engine 2, all possible: class circle {function draw () {print "circle / n";}}
Class Square {Function Draw () {Print "Square / N";}}
Function ShapeFactoryMethod ($ shape) {switch ($ shape) {copy "circle": return new circle (); case "square": return new square ();}}
ShapeFactoryMethod ("circle") -> DRAW (); ShapeFactoryMethod ("Square") -> DRAW ();?> Static member variables in static classes can now initialize EXAMPLECLASS FOO {static $ my_static = 5;}
Print foo :: $ my_static;?> The static method PHP5 introduces keyword 'static' to define a static method so that you can call from the object. Example: Class foo {public static function astaticMethod () {// ...}} foo :: astaticMethod ();?> Virtual variable $ this is invalid in a static method. InstanceOfphp5 introduces keyword instanceof to determine if an object is an instance of an object, or a derived of an object, or uses an interface. Example: Class Baseclass {}
$ a = new baseclass;
IF ($ a instanceof Basicclass) {echo "Hello World";}?> All static variables all static variables are now handled, 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. The default value is allowed to define the default value by pressing the parameters of the address transfer method: function my_function {if ($ VAR === null) {Die ("$ var needed to have a value);}} ?> __AUTOLOAD () When initializing an undefined class, the engine will automatically call the __autoload () interceptor function. This class will be passed to the __autoload () interceptor function unique parameter passed to it. Example: Function __autoload ($ classname) {include_once $ classname. ".Php";
$ Object = new classname;?>> Method and property call overload universal __call (), __get (), and __set () methods can be used to overload the method and attribute call.
Example: __get () and __set () class setter {public $ n; public $ x = array ("a" => 1, "b" => 2, "c" => 3);
Function __get ($ nm) {print "getting [$ nm] / n";
IF (isset ($ 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 () Class Caller {var $ x = array (1, 2, 3);
Function __call ($ m, $ a) {print "Method $ M CALLED: / N"; var_dump ($ a); return $ this-> x;}}