Learn php-(13) object-oriented Programming 3

Source: Internet
Author: User
Tags learn php zend framework

2.3 Member methods (functions)

The member approach I understand is to implement specific functions of this class, or what this class can do. It does not differ from functions outside the class, but is declared within the class. You need to use the instance's class object to invoke it when you are using it.

Similarly, member methods can be decorated with permission modifiers, private, protected, public. When decorated with modifiers, they use the same permissions as the member properties. If the use permission modifier is not displayed, the default is public. General member methods are declared public to facilitate object invocation and to manipulate private properties within the class.

I don't have a special example here.

2.4 Final keyword

The front has been contacted by three keywords, this,static,const. The final keyword is often used.

Classes and methods that have been modified by the keyword final are "final classes and methods." In other words, the final modified class cannot be inherited, the final modified method cannot be overridden, and the final modified property cannot be changed.

The final keyword is written in front of the class and function high keywords.

As final class myclass{

......

}

Final function MyFunction () {

......

}

3. Inheritance of Class 3.1 classJust as we can inherit the property of our fathers, classes can also inherit. With class inheritance, the inherited class is called the parent class or base class, and the inherited class is called a subclass or derived class. Subclasses can inherit all the properties and methods of the parent class, and can override methods that are not final-modified in the parent class when necessary. Class is inherited using: extends keyword. But remember that PHP is a single inheritance, which means that a class can inherit only one class, not multiple classes at a time, unlike C + +.Why use inheritance of classes?What I understand is that there are some things they belong together in a big class, have a common attribute or method, but themselves have new properties or methods, then if I use the time to write some of the same code every time, there is absolutely no need, then I would like to put their common things in a base class, Separate their different things, and then inherit the base class, so I have a lot less work to do. For a very simple example, eagles and Tigers can all be called animals, and they all have organs such as eyes and mouths to eat. This is what they have in common, but Eagles fly and Tigers run, which is their difference. Then I can declare an animal class, class animal{} put them in the same place, different I inherit this, add new methods. Of course, I can also write separately, do not inherit, but I here just to give two specific animals, if there are many, but also one to write, their common place is not necessary. For example, I recently looked at the Zend Framework framework, need to declare a lot of table models, they have inherited zend_db_table this class, if not inherit, there are many many ways to rewrite, one is not written, two is not necessary, so inheritance is very important.
Class animal{protected $eyes;p rotected $mouth,//...public function Eat () {//...}} Class Tiger extends Animal{public function run () {//...}} Class Eagle extends Animal{public function fly () {//...}}
Do not assume that only this method is declared in a subclass, but that it has all the methods and properties in the parent class. And you can use properties and methods that are not private declarations. 3.2 Abstract classAn abstract class is a class that cannot be instantiated, that is, if I declare an abstract class, it cannot be a new object of this class. It can only be used as a parent class for other classes. It uses the abstract keyword declaration:
Abstract class myclass{//...}
Abstract classes contain at least one abstract method, which is also declared using the abstract keyword, such as:
Abstract function functionname (various parameters);
The abstract method must be followed by ";". Abstract methods can not be implemented in abstract classes, that is, there is no function body, just a declaration. Its functionality can only be done in subclasses.Note here: Abstract classes can contain ordinary methods, not necessarily abstract methods, but must contain at least one abstract method.


At this point, the object-oriented basic completion. And the use of the interface, that when I use the time to write it. The next article begins to write the MySQL database operation.

Learn php-(13) object-oriented Programming 3

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.