Learning PHP while logging-(13) Object-Oriented Programming 3

Source: Internet
Author: User
: This article mainly introduces how to learn PHP-(13) Object-Oriented Programming 3 while logging. For more information about PHP tutorials, see. 2.3 Member methods (functions)

The member method I understand is to execute some specific functions of this class, or what this class can do. It is no different from functions outside the class, but it is declared in the class. You need to use the class object of the instance to call it during use.

Similarly, member methods can be modified by permission modifiers such as private, protected, and public. After the modifier is used, their permissions are the same as those of member attributes. If the permission modifier is not displayed, the default value is public. Generally, member methods are declared as public to facilitate object calls and operate private properties in the class.

Here I will not give a special example.

2.4 final keywords

The preceding three keywords are exposed: this, static, and const. The final keyword is also frequently used.

The classes and methods modified by the keyword final are "final classes and methods ". In other words, classes modified by final cannot be inherited, methods modified by final cannot be overwritten, and properties modified by final cannot be changed.

The final keyword is written before the class and function high keywords.

For example, final class MyClass {

//......

}

Final function MyFunction (){

//......

}

3. class inheritance

Inheritance of class 3.1

Just as we can inherit the property of our fathers, classes can also inherit. After the class is inherited, the inherited class is called the parent class or the base class, and the inherited class is called a subclass or a derived class. Subclass can inherit all attributes and methods of the parent class, and can override methods not modified by final in the parent class when necessary. Class inheritance use: extends keyword. But remember that PHP is a single inheritance. that is to say, a class can only inherit one class and cannot inherit multiple classes at a time. This is different from C ++.

Why should we use class inheritance?

What I understand is that some of them belong to one category and share the same attributes or methods, but they have new attributes or methods, so if I repeatedly write the same code every time when using it, it is completely unnecessary, so I should put what they share in a base class, separate their different things and inherit the base class, which reduces the workload. For example, the Hawks and tigers can both be called Animals. they both have eye and mouth organs and can eat. This is what they both have in common, but the Eagles will fly and the Tigers will run. this is their difference. Then I can declare an animal class. class animal {} puts them in the same place. different classes inherit this and add new methods. Of course, I can also write them separately without inheriting them. but I just mentioned two specific animals here. if there are many, do I have to write them one by one? there is no need to share them together. For another example, the zend framework I recently looked at needs to declare many table models. they all inherit the zend_Db_Table class. if they are not inherited, many methods need to be rewritten by themselves, first, it cannot be written. Second, it is unnecessary. Therefore, inheritance is very important.

class Animal{protected $eyes;protected $mouth;//...public function eat(){//...}}class Tiger extends Animal{public function run(){//...}}class Eagle extends Animal{public function fly(){//...}}
Do not think that only this method is declared in the subclass. it actually has all the methods and attributes in the parent class. You can also use non-private declared attributes and methods.

3.2 Abstract class

An abstract class is a class that cannot be instantiated. that is to say, if I declare an abstract class, there cannot be a new object of this class. It can only be used as the parent class of other classes. It uses the abstract keyword declaration:

abstract class MyClass{//...}
Abstract classes contain at least one abstract method. abstract methods are also declared using abstract keywords, such:

Abstract function FunctionName (various parameters );
The abstract method must be followed by ";". Abstract methods cannot be implemented in abstract classes. that is to say, they do not have function bodies, but are declared. Its functions can only be completed in child classes.

Note: the abstract class can contain common methods, not necessarily abstract methods, but must contain at least one abstract method.

At this point, object orientation is basically complete. There is also the use of the interface, that will be written when I use it. Next, we will start writing data to the MySql database.

The above describes how to learn PHP-(13) Object-Oriented Programming 3, including some content, and hope to help those who are interested in PHP tutorials.

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.