PHP Object-Oriented Programming and php Object-Oriented Programming

Source: Internet
Author: User

PHP Object-Oriented Programming and php Object-Oriented Programming
Access Control

Public: it can be accessed in the class, subclass, or outside the class.

Protected: It can only be accessed in the class itself and its subclass.

Private: only access is allowed in the declared class. private Class Members cannot directly access the quilt class or the object instance of this class.

 

Abstract classes and Methods

When the concept of inheritance is applied in some scenarios, it is not appropriate to create a parent class instance. In this case, it is more appropriate to define a parent class as an abstract base class, rather than a standard base class (abstract classes cannot be instantiated ).

Abstract Classes define some methods in the inheritance class (subclass) of this base class. Then the subclass defines the true implementation of these methods.

The difference between an abstract class and a common class is that if you try to create an object from an abstract class, a fatal error will occur. The abstract class is used for extension, and then we can create instances of these derived classes.

Definition of abstract classes and methods:
// The definition of an abstract class starts with the abstract keyword abstract class People {protected $ name = ""; // The abstract method starts with abstract, and the abstract method has no method body, end abstract public function setName ($ name);} // the class Teacher extends People {public function setName ($ name) method that implements the abstract class in the subclass) {$ this-> name = $ name ;}}

Note:

1. When an abstract method is implemented in a subclass, its visibility must be higher than or equal to the visibility defined by the abstract method. If the abstract method is public, the implementation method in the subclass must also be public. If the abstract method is protected, the subclass can only be protected or public.

2. We cannot define abstract methods as private because a private method cannot be inherited.

3. In all cases, the implementation version of the method must have the same number of parameters as the abstract method (the function signature must be consistent ).

4. All abstract methods in the base class must be implemented in the subclass. Otherwise, a fatal error will occur.

5. attributes are not abstracted. attributes in an abstract class are defined as normal attributes. subclasses can also overwrite attributes in the abstract class. In this case, the attribute visibility settings are consistent with those in the abstract method (see article 1st ).

 

This chapter will occasionally update all PHP object-oriented knowledge, so stay tuned ......

 

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.