I haven't used PHP to write programs in the past two years. today I want to use PHP. I checked it on the internet and saw PHP5. I had a new hobby, so I translated this article. The article originated at http://www.php.net /. The object processing department of the new object model PHP has not used PHP to write programs in the past two years. today, I want to use PHP and check it online. I can see PHP5, for a moment, my hobbies grew bigger, so I translated this article.
The article originated at http://www.php.net /.
New Object Model
The object processing part in PHP has been completely rewritten, with better performance and more functions. In previous PHP versions, objects were treated as original simple types.
(Such as integer and string). The problem with this method is that when the variable is assigned or passed as a parameter, the object copy is obtained. In the new version,
An object is referenced by a handle, rather than the object value (the handle is considered an object identifier ).
Many PHP programmers may not realize the "copying quirks" of the old object model, so most previous PHP programs do not need to make any changes.
You can run it, or only make few corrections.
Private and protected members
PHP 5 introduces private and protected member variables that can define visualized class attributes.
Example
Protection member variables can be visited in the subclass of this class, while private member variables can only be visited in the class.
Hello; print 'myclass: printHello ()'. $ this-> Bar; print 'myclass: printHello ()'. $ this-> Foo ;}} class MyClass2 extends MyClass {protected $ Foo; function printHello () {MyClass: printHello ();/* shocould print */print 'myclass2 :: printHello ()'. $ this-> Hello;/* Shouldn't print out anything */print 'myclass2: printHello ()'. $ this-> Bar;/* Shouldn't print (not declared) */print 'myclass2: print Hello ()'. $ this-> Foo;/* shocould print */} $ obj = new MyClass (); print $ obj-> Hello;/* No content is output, the following is similar to */print $ obj-> Bar;/* Shouldn't print out anything */print $ obj-> Foo; /* Shouldn't print out anything */$ obj-> printHello ();/* shocould print */$ obj = new MyClass2 (); print $ obj-> Hello; /* Shouldn't print out anything */print $ obj-> Bar;/* Shouldn't print out anything */print $ obj-> Foo;/* Shouldn't p Rint out anything */$ obj-> printHello ();?>
Private and protection methods
In PHP 5 (ZEND Engine 2), private methods and protection methods are also introduced.
Example:
APrivateMethod () ;}} class Bar extends Foo {public function aPublicMethod () {echo 'Bar: aPublicMethod () called. \ n'; $ this-> aProtectedMethod () ;}}$ o = new Bar; $ o-> aPublicMethod ();?>
Although the user-defined classes or methods in the previous code do not define key words such as 'public, ''protected', or 'private', they can be run without correction.
Abstract classes and methods
PHP 5 also introduces abstract classes and methods. An abstract method only declares the "symbol" of a method and does not provide its implementation. A class containing abstract methods must be declared as "abstract ".
For example:
Test ();?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service