object-oriented Programming in PHP: Ways to develop large PHP projects (II.)
Source: Internet
Author: User
The object of the "Another" class now has all the data members and methods of the parent class (something), plus its own data into
Staff 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
Use the Getx method in "something". If you declare a data member in a derived class that has the same name as Kippe, then when you handle 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 called when you create an object of a class
To use, for example:
?>--------------------------------------------------------------------------------
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 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, and uses default arguments if the incoming argument is less than the required parameter.
When an object of a derived class is created, only its constructors are invoked, the constructor of the parent class is not invoked, and if you want to invoke 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 a derived class are available.
function Another () {
$this->y=5;
$this->something ();
Show call base class constructor
}
?>--------------------------------------------------------------------------------
An excellent 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 often use abstract classes to force programmers to derive from a base class, which ensures that the new class contains some expected functionality. There are no standard methods in PHP, but:
If you need this feature, you can define the base class, and adding "die" to its constructor, which guarantees that the base class is not instantiated and now adds the "die" statement after each method (interface), so if a programmer does not overwrite the method in the derived class, an error is raised. And because PHP is untyped, you may need to verify that an object is derived from your base class, add a method to the base class's identity (return some identity id), and validate the value when you receive an object parameter. Of course, if an evil-bad programmer overrides this method in a derived class, this approach will not work, but the general problem is more of an idle 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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service