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

Source: Internet
Author: User
PHP object-oriented programming: method for developing large PHP projects (2) Author: LuisArgerich translator: limodou & quot; Another & quot; class object now has a parent class (Something) all data members and methods, and their own data members and methods are added. You can use SyntaxHigh

PHP object-oriented programming: method for developing large PHP projects (2) Author: Luis Argerich translator: limodou "Another" class object now has a parent class (Something) all the data members and methods, and add their own data members and methods. You can use $ obj2 = new Something; $ obj2-> setX (6); $ obj2-> setY (7); PHP does not support multiple inheritance yet, therefore, 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 class in the derived class, it will "hide" the data member of the base class when you process it. You can define constructors in your class. A constructor is a method with the same name as a class name. it will be called when you create a class object, for example :-------------------------------------------------------------------------------- X = $ y;} function setX ($ v) {$ this-> x = $ v;} function getX () {return $ this-> x ;}}?> Then you can create an object by: $ obj = new Something (6); the constructor automatically assigns 6 values 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") followed by: $ 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. Therefore, you cannot ignore the value of Y and give X a default parameter, parameters are assigned from left to right. if the input parameter is smaller than the required parameter, the default parameter is used for parameter assignment. 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. -------------------------------------------------------------------------------- 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, but if you need this feature, you can define the base class and add the "die" call after its constructor, this ensures 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.