PHP class declaration and instantiation and construction methods and methods of analysis, PHP detailed _php tutorial

Source: Internet
Author: User

PHP class declaration and instantiation and construction methods and methods of analysis, PHP detailed


In this paper, we describe the declaration and instantiation of PHP class, the method of construction and the method of destructor. Share to everyone for your reference, as follows:

<?phpclass human{public  static $leg =2;  Public $name = ' Leo ';  Public $age = ' + ';  Public Function Cry () {  }} $leo = new Human ();p Rint_r ($leo);/* Returns human Object ([name] = Leo [age] + 25) *///why not What about leg? Because of the addition of static, it becomes the property of the class, is all///Through this class instance instance of the object after the//public is the permission modifier, the permission modifier has public,protected,private//in PHP4 often used to Var, This is not how to recommend now, the equivalent of public//in the PHP5 will be the Var analysis into public?>

Is there any way to change the properties of an object by passing parameters at the time of the new object? Rather than the same.

A: You can define a construction method in a class that executes when the object is initialized, and can receive parameters

As shown below:

<?php class human{public  static $leg =2;  Public $name = ' Leo ';  Public $age = ' + ';  Public function __construct ($name, $age) {    $this->name= $name;    $this->age= $age;  }} $leo = new Human (' macro ', ' all ');p Rint_r ($leo);/* Returns human Object ([name] = macro [age] + 23) You can see that the parameters are working. __construct is Constructor */?>

Corresponding to the constructor is the destructor, which is executed when the object is destroyed.

As shown below:

<?php class human{public  static $leg =2;  Public $name = ' Leo ';  Public $age = ' + ';  Public function __construct ($name, $age) {    $this->name= $name;    $this->age= $age;    echo $this->name. " Object generation.
"; } Public Function __destruct () { echo $this->name. Object destroyed.
"; }} $leo = new Human (' macro ', ' all '), $tim = new Human (' Tim ', ' + '); unset ($leo); Echo ' ~~~~~~~~~~~~~~~~~~~~
*/* Return: Macro object generated Tim object generated macro object destroyed ~~~~~~~~~~~~~~~~~~~~tim object destroyed *///__destruct is a destructor, that is, when the object is destroyed call//why $tim not unset Destructors are also executed//This is an implicit destruction, unset is explicitly destroyed//when a page execution finishes automatically destroyed?>

More readers interested in object-oriented content in PHP can view the topic: "PHP Object-oriented Programming primer tutorial"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Declaration and object Instantiation of PHP Learning Note class
    • Talking about the thinkphp model of the case
    • PHP Object-oriented strategy (ii) Instantiating objects using object members
    • A little notes of the PHP instantiation class
    • PHP construction method, destructor method and this keyword in detail
    • PHP Object-oriented all-around approach (four) construction method and destructor method
    • PHP implements the constructor method and the method of overwriting for parent call

http://www.bkjia.com/PHPjc/1096147.html www.bkjia.com true http://www.bkjia.com/PHPjc/1096147.html techarticle PHP class declaration and instantiation and construction methods and methods of analysis, PHP detailed examples of this article describes the PHP class declaration and instantiation and construction methods and the method of destruction. Share for everyone to join us ...

  • Related Article

    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.