PHP Object-oriented – inheritance and rewrite _php tutorial

Source: Internet
Author: User

PHP Object-oriented-inheritance and rewriting


PHP Object-oriented-inheritance and rewriting

Inherited:

In PHP, the purpose is achieved by using special operations on the class.

By defining the class, the extends is used to indicate that the current class object inherits the object of that 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, which refers to between two objects, so where are these two objects?

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

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 that the class inherits from.

Class D extends C

Class D Objects, inherited from Class C objects.

Parent Class: Inherited class, Class C

Subclass: Classes that need to be inherited, Class D

Base class: Class C is the base class for Class D

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

Important:

PHP is a single inheritance.

Purpose of Inheritance:

is to extend, or use a class of operations and data that already exists.

overriding override

When inheriting, if a member conflict occurs, the way PHP is handled is rewritten. That is, the child is a member of a similar name that overrides the parent. You cannot see a member of the same name as the parent class.

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

Construction method Overrides:

Example:

Class P

{

Public__construct ()

{

echo "Parent::construct";

}

}

Class D extends P

{

Public__construct ()

{

echo "Self::construct";

}

}

$o =new D;

Output: Self::construct

If required, enforces the overridden parent class method, which can be displayed using the parent class to invoke 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, within a class, instead of the current parent 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 structure of the parent class requires a corresponding parameter, you need to pass the parameters required by the parent class construction method to the method at the time of invocation.

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;

}

}

http://www.bkjia.com/PHPjc/871196.html www.bkjia.com true http://www.bkjia.com/PHPjc/871196.html techarticle PHP Object-oriented – inherit and rewrite Php object-oriented – inheritance and rewrite inheritance: PHP, by using special operations on the class to achieve the purpose. By using Exte when defining a class ...

  • 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.