Programming | object | object-oriented programming for Project PHP: Methods for developing large PHP projects (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 into
and methods.
You can use
$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 than two classes.
you can redefine a method in a derived class, and if we redefine the Getx method in the "Another" class, we can't make the
uses the Getx method of "something". If you declare a data member in a derived class that has the same name as Kippe, then when you deal with it,
it will "hide" the data members of the base class.
you can define constructors in your class. A constructor is a method that has the same name as a class name and is
when you create an object of a class
The
constructor automatically assigns 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")
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
The default parameter uses C + +, so you can't ignore the value of Y, and give X a default parameter, which is assigned from left to right if
the parameters passed in are less than the required parameters, they will use the default parameters.
when an object of a derived class is created, only its constructors are invoked, and the constructor of the parent class is not invoked, if you want to call the base
class constructor, 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 a derived class are
A good mechanism for
oop is to use abstract classes. An abstract class is not instantiated and can only be supplied to a derived class as an interface. Designers usually
uses abstract classes to force programmers to derive from a base class, which ensures that the new class contains 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
The
class is not instantiated and is now preceded by a "die" statement after each method (interface), so if a programmer does not have
in a derived class
overrides method, an error is raised. And because PHP is untyped, you may need to verify that an object is derived from your base class
class, add a method in the base class to the identity of the real class (return some identity id) and validate
when you receive an object parameter
this value. Of course, if an evil-bad programmer overrides this method in a derived class, this method will not work, but the general
The
problem is more of a lazy programmer than an evil one.
Of course, it's nice to be able to make the base class impossible for programmers to see, just print the interface to do their job.
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.