In PHP object-oriented language, class definition and Object Instantiation operations, as well as special usage of constructor and destructor, php object-oriented language

Source: Internet
Author: User

In PHP object-oriented language, class definition and Object Instantiation operations, as well as special usage of constructor and destructor, php object-oriented language

Class Definition and Object Instantiation

<? Php
// The members in the class belong to the object
Class Hero {
Public $ name; // member variable
// Member attributes (in strong language)
Protected $ blood;
Private $ attack;
// Member Method
Function skill (){
Echo "$ this-> name <br> control ";
}
}

$ S = new Hero (); // create a Hero
$ S-> name = "Riven"; // assign a value to the hero
Echo "$ s-> name <br>"; // output the hero name
$ S-> skill ();

Constructor and destructor
// Function: Initialize some Members in the class (custom initial values of parameters)
Class Ren {
Public $ name;
Public $ age; // type used in strongly typed languages

// Old version definition usage
/* Public function Ren (){
Echo "constructor ";
Public function Ren ($ v ){
$ This-> age = $ v ;*/

// PHP New Version definition usage
Public function _ construct ($ v ){
$ This-> age = $ v;
}
Public function _ destruct (){
Echo "destroying files ";
}
}
$ S = new Ren (20 );
Echo $ s-> age;
?>

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.