PHP's object-oriented programming method for developing large PHP projects second reprint _php tutorial

Source: Internet
Author: User
Object-oriented Programming in PHP: How to develop a large PHP project (ii) Author: Luis Argerich Translator: Limodou The object of the "another" class now has all the data members and methods of the parent class (Something), plus its own data members and methods.   You can use the $obj 2=new Something;   $obj 2->setx (6);   $obj 2->sety (7);   PHP does not now support multiple inheritance, so you cannot derive new classes from two or more of the two classes. You can redefine a method in a derived class, and if we redefine the Getx method in the "Another" class, we cannot use the Getx method in "Something".   If you declare a data member in a derived class that has the same name as implementation, it will "hide" the data members of the base class when you process it. You can define constructors in your class. A constructor is a method that has the same name as a class, and is called when you create an object of a class, for example:--------------------------------------------------------------------------- ----- X= $y; } function SetX ($v) {$this->x= $v;} function GetX () {return $this->x;}} ?>--------------------------------------------------------------------------------So you can create an object by: $obj =new   Something (6); The constructor automatically assigns a value of 6 to the data variable, x.   Constructors and methods are normal PHP functions, so you can use the default parameters. function Something ($x = "3", $y = "5") Then: $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 default parameters use C + +, so you can't ignore the Y value, and give X a default parameter, the parameter is assigned from left to right, if the passed parameter is less than the required parameter, it will use the default parameter. When an object of a derived class is created, only its constructor is called, the constructor of the parent class is not called, and if you want to call the constructor of the base class, you must display the call in the constructor of the derived class. You can do this because the methods of all the parent classes in the derived class are available. -------------------------------------------------------------------------------- y=5; $this->something ();   Show call base class constructor}?>-------------------------------------------------------------------------------- A good mechanism for OOP is to use abstract classes. Abstract classes are not instantiated and can only be supplied to a derived class interface. Designers often use abstract classes to force programmers to derive from base classes, which ensures that new classes contain some expected functionality. There is no standard method in PHP, but: if you need this feature, you can define the base class, and add a "die" call after its constructor, so that the base class is not instantiated, and now after each method (interface) with a "Die" statement, so, If a programmer does not overwrite a method in a derived class, an error is raised. And because PHP is untyped, you might want to make sure that an object is derived from your base class, then add a method in the base class to the identity of the literal class (returning an Identity ID), and verify the value when you receive an object parameter.   Of course, if an evil, bad programmer overrides this method in a derived class, this method does not work, but the general problem is that it is more of a lazy programmer than an evil one.   Of course, it's good for the base class to be impossible for programmers to see, as long as the interface is printed out to do their job. There are no destructors in PHP. Transfer from phpbuilder.com

http://www.bkjia.com/PHPjc/532249.html www.bkjia.com true http://www.bkjia.com/PHPjc/532249.html techarticle object-oriented programming in PHP: How to develop a large PHP project (ii) Author: Luis Argerich Translator: Limodou Another class objects now have all the data members and parties of the parent class (Something) ...

  • 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.