PHP Object-oriented? Final class

Source: Internet
Author: User
PHP Object-oriented? Final class

The class, which can only be instantiated by an object, cannot be used for inheritance.

At design time, the class can no longer be extended, it should be restricted by the final syntax, and other users should extend the class.

Defined:

Before class, add the final keyword.

Example:

Class Goods

{

Public $goods _name;

Public $shop _price;

Public function __construct ($name, $price)

{

$this->goods_name= $name;

$this->shop_price= $price;

}

}

Final class Goodsbook extends Goods

{

Public $pages;

Public function __construct ($name, $price, $pages)

{

Parent::__construct ($name, $price);

$this->pages= $pages;

}

}

$book 1 = new Goodsbook (' php ', 234,56,45);

Another usage of the Final keyword for restricting methods:

Restricts the method, which cannot be overridden when the owning class is inherited.

Example:

Class Goods

{

Public $goods _name;

Public $shop _price;

Public function __construct ($name, $price)

{

$this->goods_name= $name;

$this->shop_price= $price;

}

Public Function Sayname ()

{

Echo $this->goods_name;

}

All goods should be priced in the same way

Final public Function Sayprice ()//Inherit the class, the method cannot be overridden

{

Echo ' ¥ ', $this->shop_price;

}

}

Final class Goodsbook extends Goods

{

Public $pages;

Public function __construct ($name, $price, $pages)

{

Parent::__construct ($name, $price);

$this->pages= $pages;

}

Public Function Sayname ()

{

echo "$this->goods_name";

}

}

$book 1 = new Goodsbook (' php ', 234,56,45);

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