Method 2 of PHP object-oriented programming for large-scale PHP Project Development

Source: Internet
Author: User
Author: LuisArgerich translator: limodou & quot; Another & quot; class objects now have all the data members and methods of the parent class (Something, in addition, we have added our own data members and methods. You can use the $ obj2newSomething; $ obj2-setX (Syn

Author: Luis Argerich translator: limodou
The object of the "Another" class now has all the data members and methods of the parent class (Something), and adds its own data
Personnel and methods.
You can use
$ Obj2 = new Something;
$ Obj2-> setX (6 );
$ Obj2-> setY (7 );
PHP currently does not support multi-inheritance, so you cannot derive a new class from two or more classes.
You can redefine a method in a derived class. if we redefine the getX method in the "Another" class, we cannot
Use the getX method in "Something. If you declare a data member with the same name as the base in the derived class, when you process it,
It hides the data members of the base class.
You can define constructors in your class. A constructor is a method with the same name as a class name. when you create a class object, it is called
For example:
--------------------------------------------------------------------------------
Class Something {
Var $ x;
Function Something ($ y ){
$ This-> x = $ y;
}
Function setX ($ v ){
$ This-> x = $ v;
}
Function getX (){
Return $ this-> x;
}
}
?> --------------------------------------------------------------------------------
Therefore, you can create an object:
$ Obj = new Something (6 );


The constructor automatically assigns value 6 to the data variable x. Constructors and methods are common PHP functions, so you can use the default parameters.
Function Something ($ x = "3", $ y = "5 ")
Next:
$ Obj = new Something (); // x = 3 and y = 5
$ Obj = new Something (8); // x = 8 and y = 5
$ Obj = new Something (8, 9); // x = 8 and y = 9
The default parameter uses the C ++ method, so you cannot ignore the value of Y. Instead, assign a value to X from left to right, if the input parameter is less than the required parameter, the default parameter is used for the input parameter.
When an object of a derived class is created, only its constructor is called, and the constructor of the parent class is not called. if you want to call the constructor of the base class, you must display the call in the constructor of the derived class. This can be done because all methods of the parent class in the derived class are available.
--------------------------------------------------------------------------------
Function Another (){
$ This-> y = 5;
$ This-> Something ();
// Display the call base class constructor
}
?> --------------------------------------------------------------------------------
A good mechanism of OOP is to use abstract classes. Abstract classes cannot be instantiated and can only be provided to an interface of the derived class. Designers usually use abstract classes to force programmers to derive from the base class. This ensures that the new class contains some expected functions. There is no standard method in PHP,:
If you need this feature, you can define the base class and add the "die" call to its constructor to ensure that the base class cannot be instantiated, now, the "die" statement is added after each method (interface). Therefore, if a programmer does not overwrite the method in the derived class, an error is thrown. And because PHP is non-typed, you may need to confirm that an object is derived from your base class, then add a method to the base class to implement the class identity (return a certain id), and verify this value when you receive an object parameter. Of course, if an evil and bad programmer overwrites this method in a derived class, this method does not work, but generally the problem occurs frequently in the lazy programmer, rather than the evil programmer.
Of course, it is good to make the base class invisible to programmers, as long as the interface is printed out for their work.
No Destructor in PHP.

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.