PHP5 authoritative programming reading and learning notes with e-book download _ PHP Tutorial

Source: Internet
Author: User
Download an ebook from PHP5's leading programming reading notes. Php5?programming (php5powerprogramming##www.jb51.netbooks28207.html PHP4, do not use _ construct () as the name of the constructor, must use the class name PHP 5 authoritative Programming (PHP 5 Power Programming) PDF http://www.jb51.net/books/28207.html

In PHP4, _ construct () is not used as the name of the constructor. the class name must be used to define a method, just like in C ++.
In PHP5, use the new unified constructor naming method __construct (). of course, you can also use the class name.
However, if you use both of them, the system uses the _ construct () format by default.

The code is as follows:


Class Person {
// Method in PHP4
Public function Person (){
Echo "methods in PHP4 ";
}
// Recommended method for PHP5
Public function _ construct (){
Echo "recommended methods for PHP5 ";
}
Public function say (){
}
}
$ P1 = new Person ();
?>


A constructor cannot return values. Therefore, the most common way to generate an error from the constructor is to throw an exception.
The code is as follows:

The code is as follows:


Class Person {
Private $ _ age;
Public function _ construct ($ age ){
Try {
If ($ age <120 ){
$ This-> _ age = $ age;
} Else {
Throw new Exception ("the age you entered is too large ");
}
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
}
}
$ P1 = new Person (121 );
?>


Access control
Access Protection of object attributes is a key example of OOP.
Public: can be accessed anywhere
Protected: a class member can be accessed by its subclass and its parent class from the internal method of the object.
Private: a class member can only be accessed by its class from the internal method of the object, but cannot be accessed from the member of the inherited class. Because private members are not inherited, you can declare a private variable with the same name for the two classes.
That is, both classes can only see their own private attributes, and there is no relationship between private members.
Example:

The code is as follows:


/**
* Define MyClass
*/
Class MyClass
{
Public $ public = 'public ';
Protected $ protected = 'protected ';
Private $ private = 'private ';
Function printHello ()
{
Echo $ this-> public;
Echo $ this-> protected;
Echo $ this-> private;
}
}
$ Obj = new MyClass ();
Echo $ obj-> public; // This line can be executed normally.
Echo $ obj-> protected; // This line produces a fatal error
Echo $ obj-> private; // This line also produces a fatal error.
$ Obj-> printHello (); // output Public, Protected, and Private
/**
* Define MyClass2
*/
Class MyClass2 extends MyClass
{
// Public and protected can be redefined, but private cannot
Protected $ protected = 'protected2 ';
Function printHello ()
{
Echo $ this-> public;
Echo $ this-> protected;
Echo $ this-> private;
}
}
$ Obj2 = new MyClass2 ();
Echo $ obj-> public; // This line can be executed normally.
Echo $ obj2-> private; // no private defined
Echo $ obj2-> protected; // This line produces a fatal error
$ Obj2-> printHello (); // output Public and Protected2, but Private


Note: all methods in the class must be defined using the public, protected, or private keywords. If these keywords are not set, the method is set to the default public.
Static method
Static methods can be called without creating an object instance by class name: static method, or by using the $ this-> static method or self :: static method to call.

The code is as follows:


Class Foo
{
Public static $ my_static = 'foo ';
Public static function staticValue (){
Return self: $ my_static; // use the self keyword to access static members in the class
}
}
$ Obj = new Foo ();
Echo $ obj-> staticValue (); // method 1
Echo Foo: staticValue (); // method 2
?>


Clone object
In PHP4, when a new object is returned, "object itself" is returned"
In PHP5, when a new object is added, The Returned "handle pointing to the object"
This means that in PHP5, when an object instance ($ obj1) is assigned to another variable ($ obj2), both objects point to the same memory area.
For example:

The code is as follows:


Class test {
Public $ str;
}
$ Obj1 = new test ();
$ Obj1-> str = "obj1 ";
$ Obj2 = $ obj1;
$ Obj2-> str = "obj2 ";
Echo $ obj1-> str; // output "obj1"
?>


Because $ obj1 and $ obj2 point to the same memory area, when you use any object to modify the value of the member variable, it will affect the other object.
However, in some cases, we do need to copy an object (two independent memory regions). at this time, we can use the language command clone
Refer to the following example;

The code is as follows:


Class test {
Public $ str;
}
$ Obj1 = new test ();
$ Obj1-> str = "obj1 ";
$ Obj2 = clone $ obj1;
$ Obj2-> str = "obj2 ";
Echo $ obj1-> str; // output "obj2"
?>


Parent: And self ::
Self: points to the current class and is usually used to access static members, methods, and constants.
Parent: points to the parent class. it is often used to call the constructor and method of the parent class. It can also be used to access the members and constants of the parent class.
Note: You should use parent: instead of a specific name of the parent class, because this allows you to easily change the hierarchy of your class.
Example:

The code is as follows:


Class Father {
Public function _ construct (){
Echo "calls the constructor of the parent class
";
}
}
Class Son extends Father {
Public function _ construct (){
Parent: :__ construct (); // method 1
// Father: :__ construct (); // method 2
Echo "calls subclass constructor ";
}
}
$ Son = new Son ();
?>


Result:
Call the constructor of the parent class
Call the constructor of a subclass
Recommended Method 1. the reason is described above.
Instanceof instance

The code is as follows:


Class Rectangle {
Public $ name =__ CLASS __;
}
Class Square extends Rectangle {
Public $ name =__ CLASS __;
}
Class Circle {
Public $ name =__ CLASS __;
}
Function checkIfRectangle ($ shape ){
If ($ shape instanceof Rectangle ){
Echo $ shape-> name;
} Else {
Echo "this object is not an instance of the Rectangle class ";
}
}
CheckIfRectangle (new Square (); // output: Square
CheckIfRectangle (new Circle (); // output: This object is not an instance of the Rectangle class.
?>


Note: __class _ is a special constant used to store the name of the current CLASS.

Http://www.bkjia.com/PHPjc/325684.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325684.htmlTechArticlePHP 5 authoritative Programming (PHP 5 Power Programming) PDF http://www.jb51.net/books/28207.html PHP4, do not use _ construct () as the name of the constructor, must use the class name...

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.