Author: Luis Argerich Translator: limodou
The ANOTHER class is now with all the data members and methods of the parent class, and their own data bearer and method are added.
you can use
$ Obj2 = new Something;
$ OBJ2-> SetX (6);
$ OBJ2-> Sety (7);
PHP now does not support multiple inheritance, so you can't derive new classes from two or more classes.
You can define a method in derived class, if we have defined a getx method in the "Another" class, we can't use the getx method in "Something". If you declare a data member with the same name as the base class, it will "hide" the data of the base class when you process it.
You can define constructor in your class. The constructor is a method with the same name with the class name, when you create a class object, for example:
-------------------------------------------------- ---
Class Something {
VAR $ x;
Function Something ($ Y) {
$ this-> x = $ y;
}
Function setX ($ V) {
$ this-> x = $ V;
}
Function getX () {
Return $ THIS-> X;
}
}
?> --------------------------------------------------------------------------------------------- ---
So you can create an object, pass:
$ Obj = new Something (6);
The constructor automatically assigns 6 to the data variable x. The constructor and methods are ordinary 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 thing (8); // x = 8 and y = 5
$ Obj = new Something (8, 9); // x = 8 and y = 9
The default parameter uses C mode, so you can't ignore the value of Y, and give x a default parameter, parameter is assigned from left to right, if
When the incoming parameter is less than the required parameters, the default parameters will be used.
When a derived object is created, only its constructor is called, the constructor of the parent class is not called, if you want to call the base
Class constructor, you have to display calls in the derived class constructor. It can be done because all the methods in the derived class are available.
-------------------------------------------------- ---
Function another () {
$ THIS-> Y = 5;
$ this-> something ();
// Display the invoked base class constructor
}
?> --------------------------------------------------------------------------------------------- ---
A good mechanism for OOP is to use an abstract class. Abstract classes cannot be instantiated, only one interface to the derived class. Designers typically use abstract classes to force programmers to derive from the base class, which ensures that new classes contain some expectations. There is no standard method in PHP, but:
If you need this feature, you can define the base class and add "DIE" to the "DIE" after its constructor, so that the base class can be inexhaising, and now in each method (interface) "DIE" statement, so if a programmer has no overlay in the derived class, an error will be triggered. And because PHP is not type, you may need to confirm that an object is derived from your base class, then adding a method in the base class to the identity of the class (return some kind of identity ID), and in you Check this value when you receive an object parameter. Of course, if an evil programmer covers this method in the derived class, this method does not work, but general problems are discovered in lazy programmers, not evil programmers. Of course, it is good to let the base class can't see the programmer, as long as the interface is printed to make their work.
There is no destructor in the PHP.