Basic knowledge of PHP: Object-Oriented Programming

Source: Internet
Author: User

1. Definition of Class

    • The class keyword + class name +{}, in curly braces, defines variables and methods.
    • The variables of the class are declared with Var, or the values can be initialized.
    • After a class is created, you can use the new operator to instantiate an object of that class.
    • Note: The variable $this the object that represents itself.

2. member functions

    • Constructor: Used to initialize an object when it is created, to assign an initial value to an object member variable, and to use it with the new operator in the statement that creates the object.
      void construct ([ mixed $args [, $... ]] )
    • destructor: When an object ends its life cycle, such as when the object's function has been called, the system automatically executes the destructor.
      void destruct ( void )

3. Inheritance

    • PHP uses the keyword extends to inherit a class that does not support multiple inheritance.
      Class A extends b{} //a classes inherit the class B
    • PHP does not automatically invoke the constructor of the parent class in the constructor method of the subclass. To execute the constructor of the parent class, you need to call Parent::construct () in the constructor method of the child class.
    • Method overrides: If a method inherited from a parent class does not meet the requirements of a subclass, it can be overridden, a procedure called method overrides, also known as a method.

4. Access control

    • The access control of a property or method is implemented by PHP by adding the keyword public (publicly), protected (protected), or private.
    • Class properties must be defined as public, protected, and private. If defined with Var, it is considered public.
    • A method in a class can be defined as public, private, or protected. If these keywords are not set, the method defaults to public.

5. Interface

    • Specifies which methods a class must implement, but does not need to define the specific contents of these methods.
    • The interface is defined by the interface keyword, and the method defined must be public.
      Interface A{} * //声明一个A接口*
    • To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.
      class B implements A,C //实现A,C接口

Basic knowledge of PHP: Object-Oriented Programming

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.