PHP object-oriented programming advanced features (interface, inheritance, abstract class, destructor, clone, etc.), c abstract class destructor

Source: Internet
Author: User

PHP object-oriented programming advanced features (interface, inheritance, abstract class, destructor, clone, etc.), c abstract class destructor

This article describes the advanced features of PHP object-oriented programming. We will share this with you for your reference. The details are as follows:

Static attributes

<? Phpclass StaticExample {static public $ aNum = 0; // static common property static public function sayHello () {// static common method print "hello" ;}} print StaticExample :: $ aNum; StaticExample: sayHello ();?>

Output: 0 hello

Comment: static attributes and methods can be directly called through classes.

SELF

<? Phpclass StaticExample {static public $ aNum = 0; static public function sayHello () {// The Order of static and public can be reversed here: $ aNum ++; print "hello (". self: $ aNum. ") \ n"; // self points to the current class, $ this points to the current object. } StaticExample: sayHello ();?>

Output:

hello (1)hello (2)hello (3)

Comment: self points to the current class, and this points to the current object. Self can call the static attributes and methods of the current class. This points to the current object. Self can call the static attributes and methods of the current class. This can call the normal attributes and methods of the current class.

Constant attribute

<? Phpclass ShopProduct {const AVAILABLE = 0; // you can only name the constant const OUT_OF_STOCK = 1; public $ status;} print ShopProduct: AVAILABLE;?>

Output: 0

Comment: constants can only contain uppercase letters and can be directly called through classes.

Interface

<? Phpinterface Chargeable {// interface. The abstract class is something between the base class and the interface. public function getPrice ();} class ShopProduct implements Chargeable {//... protected $ price ;//... public function getPrice () {return $ this-> price ;}//...} $ product = new ShopProduct ();?>

If the getPrice method is not implemented, an error is returned.

Fatal error: Class ShopProduct contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Chargeable: getPrice)

Inheritance class and Interface

<? Phpclass TimedService {} interface Bookable {} interface Chargeable {} class Consultancy extends TimedService implements Bookable, Chargeable {// inheritance class and interface //...}?>

Abstract class

Let's take a look at a piece of code.

<?phpabstract class DomainObject {}class User extends DomainObject {  public static function create() {    return new User();  }}class Document extends DomainObject {  public static function create() {    return new Document();  }}$document = Document::create();print_r( $document );?>

Output:

Document Object()

Static Method

<? Phpabstract class DomainObject {private $ group; // private Attribute group public function _ construct () {$ this-> group = static: getGroup (); // static class} public static function create () {return new static ();} static function getGroup () {// static method return "default ";}} class User extends DomainObject {} class Document extends DomainObject {static function getGroup () {// changed the content return "document" ;}} class SpreadS After heet extends Document {// inherits, the group is the same as the document} print_r (User: create (); print_r (SpreadSheet: create ();?>

Output:

User Object(  [group:DomainObject:private] => default)SpreadSheet Object(  [group:DomainObject:private] => document)

Final Field

Makes the class unable to be inherited, and does not use much

<? Phpfinal class Checkout {// termination class inheritance //...} class IllegalCheckout extends Checkout {//...} $ checkout = new Checkout ();?>

Output:

Fatal error: Class IllegalCheckout may not inherit from final class (Checkout)

The final method cannot be overwritten.

<? Phpclass Checkout {final function totalize () {// calculate bill} class IllegalCheckout extends Checkout {function totalize () {// The final method cannot be rewritten. // change bill calculation }}$ checkout = new Checkout ();?>

Output:

Fatal error: Cannot override final method Checkout: totalize ()

Destructor

<? Phpclass Person {protected $ name; private $ age; private $ id; function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age;} function setId ($ id) {$ this-> id = $ id;} function _ destruct () {// destructor if (! Empty ($ this-> id) {// save Person data print "saving person \ n";} if (empty ($ this-> id )) {// save Person data print "do nothing \ n" ;}}$ person = new Person ("bob", 44); $ person-> setId (343 ); $ person-> setId (''); // The final execution of the destructor. after use, run?>

Output:

Do nothing

_ Clone method

Run the command during cloning.

<? Phpclass Person {private $ name; private $ age; private $ id; function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age;} function setId ($ id) {$ this-> id = $ id;} function _ clone () {// execute $ this-> id = 0 ;}$ person = new Person ("bob", 44) during cloning; $ person-> setId (343 ); $ person2 = clone $ person; print_r ($ person); print_r ($ person2);?>

Output:

Person Object(  [name:Person:private] => bob  [age:Person:private] => 44  [id:Person:private] => 343)Person Object(  [name:Person:private] => bob  [age:Person:private] => 44  [id:Person:private] => 0)

Let's look at another example.

<? Phpclass Account {// Account class public $ balance; // balance function _ construct ($ balance) {$ this-> balance = $ balance;} class Person {private $ name; private $ age; private $ id; public $ account; function _ construct ($ name, $ age, Account $ account) {$ this-> name = $ name; $ this-> age = $ age; $ this-> account = $ account;} function setId ($ id) {$ this-> id = $ id ;} function _ clone () {$ this-> id = 0 ;}}$ person = New Person ("bob", 44, new Account (200); // use a class object as the parameter $ person-> setId (343); $ person2 = clone $ person; // give $ person some money $ person-> account-> balance + = 10; // $ person2 sees the credit tooprint $ person2-> account-> balance; // The property account of person is also a class. The value of its property balance is 210 // output: // 210?>

Comments: learning can still develop the brain. Today, I finally understand why there are multiple arrows. $ person-> account-> balance. The account attribute here is an object.

_ ToString

<? Phpclass Person {function getName () {return "Bob";} function getAge () {return 44;} function _ toString () {$ desc = $ this-> getName (). "(age"; $ desc. = $ this-> getAge (). ")"; return $ desc; }}$ person = new Person (); print $ person; // centralized processing during printing // Bob (age 44)?>

Comment: it must be print or echo, and print_r will output the object.

Person Object ()

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.