Home  >  Article  >  Backend Development  >  Learn PHP while memorizing-(13) Object-oriented programming 3

Learn PHP while memorizing-(13) Object-oriented programming 3

WBOY
WBOYOriginal
2016-08-08 09:32:29755browse

2.3 Member methods (functions)

I understand that member methods are to specifically perform certain functions of this class, or what this class can do. It is no different from a function outside the class, it is just declared inside the class. When using it, you need to use the class object of the instance to call it.

Similarly, member methods can also be modified by permission modifiers, private, protected, and public. When modified with modifiers, their usage rights are the same as those of member attributes. If the usage permission modifier is not shown, it defaults to public. Generally, member methods are declared public to facilitate object calls to operate private properties in the class.

I won’t give any specific examples here.

2.4 final keyword

I have already touched on three keywords, this, static, and const. The final keyword is also often used.

The classes and methods modified by the keyword final are the "final classes and methods". That is to say, classes modified by final cannot be inherited, methods modified by final cannot be overridden, and properties modified by final cannot be changed.

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

such as final class MyClass{

//…

}

final function MyFunction(){

//…

}

3. Class Inheritance of

3.1 Inheritance of classes

Just like we can inherit the property of our parents, classes can also be inherited. After using 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 properties and methods of the parent class, and when necessary, can override methods in the parent class that have not been finalized. Class inheritance uses the :extends keyword. But remember that PHP is single inheritance, which means that a class can only inherit one class, and cannot inherit multiple classes at once, which is different from C++.

Why should we use class inheritance?

What I understand is that some things belong to the same category and have common attributes or methods, but they have new attributes or methods themselves. So if I write the same thing every time when I use it, Code is completely unnecessary, so I might as well put their common things in a base class, separate their different things, and then inherit the base class, so that I can reduce a lot of workload. To give a very simple example, we can both call eagles and tigers animals. They both have eyes and mouths and other organs, and both can eat. This is what they both have in common, but the eagle can fly and the tiger can run. This is their difference. Then I can declare an animal class, class animal{} puts the same ones here, and the different ones I inherit this and add new methods. Of course, I can also write them separately without inheritance, but I only cited two specific animals here. If there are many, do I need to write them one by one? There is no need for them to have something in common. For another example, the zend framework I looked at recently needs to declare many table models. They all inherit the zend_Db_Table class. If they don't inherit it, many of the methods in it need to be rewritten by themselves. Firstly, they cannot be written, and secondly, they are unnecessary. , so inheritance is still 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(){
		//...
	}
}
Don’t think that this method is the only one declared in the subclass. It actually has all the methods and attributes in the parent class. And you can use non-privately declared properties 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, I cannot create a new object of this class later. It can only be used as a parent class of other classes. It uses the abstract keyword to declare:

abstract class MyClass{
//...
}
The abstract class contains at least one abstract method, and the abstract method is also declared using the abstract keyword, such as:

abstract function FunctionName(各种参数);
The abstract method must be followed by ";". Abstract methods cannot be implemented in abstract classes, that is to say, there is no function body, only declaration. Its functionality can only be accomplished in subclasses.

Note here: Abstract classes can contain ordinary methods, not necessarily abstract methods, but they must contain at least one abstract method.

At this point, object-oriented is basically completed. There is also the use of the interface, I will write that when I need it. The next article will start writing the operations of the MySql database.

The above has introduced learning PHP while memorizing - (13) Object-oriented Programming 3, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn