Photoshop Learning Web PHP Learning Notes Object-oriented construction and destructor method

Source: Internet
Author: User
Copy CodeThe code is as follows:


/*
* 1. Access to members of the object (in an internal method of an object, to access the other methods and member properties in this pair of authoritative)
* 2. The method in the object has a $this keyword, which represents the object that called the method
*
* Construction Method
*
* 1. Is the "first" "Auto-call" method after object creation is complete
*
* 2. The definition of the constructor method, the method name is a fixed,
* in PHP4: The same method as the class name is the construction method
* in PHP5: The construction method is selected using the Magic Method __construct () All classes declare the constructor method using this name
* Advantages: The construction method does not change when changing the class name
* Magic method: In the class to write a magic method, the method corresponding to the function will add
* Method names are fixed (both are system-provided) and do not have their own defined
* Every Magic method is a method that is called automatically at different times in order to complete a function
* Different magic methods have different calling timing
* It's all a way to start with __
* __CONSTRUCT (); __destruct (); __set ();
*
* Function: Initialize the member property;
*
*
* Destructor method
*
* 1. Method that the last "automatic" call occurs before the object is released
* Use the garbage collector (Java PHP), and C + + manual release
*
* Role: Close some resources and do some cleanup work
*
* __DESTRUCT ();
*
*/
Class person{
var $name;
var $age;
var $sex;
The method of construction in PHP4
/*function person ()
{
Each object declared will be called
echo "1111111111111111";
}*/
The method of construction in PHP5
function __construct ($name, $age, $sex) {
$this->name= $name;
$this->age= $age;
$this->sex= $sex;
}
function say () {
$this access to members of the->name;//object using $this
echo "My name: {$this->name}, my age: {$this->age}
"
}
function Run () {
}
Function Eat () {
}
destructor method
function __destruct () {
}
}
$p 1=new person ("Zhangsan", 25, "male");
$p 2=new person;
$p 3=new person;

The above describes the Photoshop Learning Web site PHP Learning notes Object-oriented construction and destruction methods, including the Photoshop learning site content, I hope that the PHP tutorial interested in a friend to help.

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