Php object-oriented-inheritance and rewriting

Source: Internet
Author: User

Php object-oriented-inheritance and rewriting

Php object-oriented-inheritance and rewriting

Inheritance:

In php, you can use special operations on the class to achieve the goal.

You can use extends to specify the class object that the current class object inherits when defining the class.

Example:

Class C

{

Public $ p_c = "value c ";

}

Class D extends C

{

Public $ p_d = "value d ";

}

$ O = new D;

Var_dump ($ o-> p_c)

Var_dump ($ o-> p_d)

Output: string (7) "value c" string (7) "value d"

Inheritance refers to the relationship between two objects. How can there be these two objects?

Instanceof operator (determines whether an object is an instance of a certain type)

Var_dump ($ o instanceof D );

Output: bool (true)

Var_dump ($ o instanceof C );

Output: bool (true)

Therefore, an object is an instance of the current class and an instance of the class inherited by this class.

Class D extends C

Class D Objects inherited from Class C objects.

Parent class: inherited Class, Class C

Subclass: class to be inherited, Class D

Base Class: Class C is the base class of class D.

Extension class: Class D is an extension class of class C.

Important:

Php is a single inheritance.

Purpose of inheritance:

It is an extension or an existing operation or data type.

Override

If a member conflict occurs during inheritance, the php method is rewritten. That is, the member with the same name of the Child class overwrites the member with the same name of the parent class. You cannot see members of the parent class with the same name.

Example:

1.

Class P

{

Public $ name = 'P ';

}

Class C extends P

{

Public $ name = "C ";

}

$ O = new C;

Echo $ o-> name;

2.

Class P

{

Public $ name = 'P ';

Public function sayName ()

{

Echo 'parent: name', $ this-> name;

}

}

Class C extends P

{

Public $ name = "C ";

Public function sayName ()

{

Echo 'self: name', $ this-> name;

}

}

$ O = new C;

$ O-> sayName ();

Output: self: name C

Constructor override:

Example:

Class P

{

Public _ construct ()

{

Echo "parent: construct ";

}

}

Class D extends P

{

Public _ construct ()

{

Echo "self: construct ";

}

}

$ O = new D;

Output: self: construct

If you need to execute the override parent class method, you can use the parent class to call the corresponding parent class method:

Example:

Class P

{

Public _ construct ()

{

Echo "parent: construct ";

}

}

Class D extends P

{

Public _ construct ()

{

P ::__ construct ();

Echo "self: construct ";

}

}

$ O = new D;

Output: parent: construct self: construct

You can use a keyword to replace the current parent class in the class.

Parent keyword

Example:

Class P

{

Public _ construct ()

{

Echo "parent: construct ";

}

}

Class D extends P

{

Public _ construct ()

{

Parent: :__ construct ();

Echo "self: construct ";

}

}

$ O = new D;

If the construction of the parent class requires corresponding parameters, you need to pass the parameters required by the parent class constructor to the method during the call.

Example:

Class Goods

{

Public $ goods_name;

Public $ goods_price;

Public function _ construct ($ name, $ price)

{

$ This-> goods_name = $ name;

$ This-> goods_price = $ price;

}

}

Class GoodsBook extends Goods

{

Public $ pages;

Public function _ construct ($ name, $ price, $ pages)

{

Parent: :__ construct ($ name, $ price );

$ This-> pages = $ pages;

}

}

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.