Php object-oriented-inheritance and rewriting, php object-oriented Rewriting

Source: Internet
Author: User

Php object-oriented-inheritance and rewriting, php object-oriented 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;

}

}


PHP object-oriented and method rewriting problems?

Class A {public function x () {echo 'x () ';} class AA extends A {public function x () {$ args = func_get_args (); if (count ($ args)> 0) {echo 'x ('. $ args [0]. ')';} else {parent: x () ;}}a: x (); AA: x (1); AA: x (); that's what it means after you try it.


What is the difference between inheriting an abstract class in PHP and inheriting a common class?

An abstract class must contain an abstract method and can only be handed over to its inherited class for rewriting. For example, if you use the abstract class above, the inherited class must have a kx method. The inheritance class of the common class below does not have the kx method. The advantage of this is uniformity. For example, we need to have a database operation class, which may be used to operate mysql or oracle, but they all have some common methods, in this way, we use an abstract class to unify methods and parameters. Both the mysql class and the oracle class inherit this abstract class. In this way, we will operate the mysql class in the future, oracle is an instance of the oracle class, but the same methods and parameters can be used for both mysql and oracle operations (for example, the system provides the database abstraction layer such as PDO for our use ). Of course, you can also say that the inheritance of common classes can also unify the method parameters. Indeed, if the successors do not have a uniform method, no error will be reported. The use of abstract classes requires that the method must be rewritten; otherwise, an error occurs.

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.