PHP Object-oriented learning notes – constructs, destructors, object assignments, clones

Source: Internet
Author: User

PHP Object-oriented learning notes – constructs, destructors, object assignments, clones

Class student

{

Public $stu _id; Defining member variables

Public $stu _name;

Public Function Sayname ()//member function

{

Echo $this->stu_name; $this accessing the member variables of the object

}

}

$stu = new Student; Creating objects

$stu->stu_id = ' 0607001 ';

$stu->stu_name = ' Xiao Li ';

$stu->sayname ();

Attention:

Class student

{

Public $stu _id; Defining member variables

Public $stu _name;

Public Function Sayname ()//member function

{

Var_dump ($stu _name);

Var_dump ($GLOBALS [' stu_name ']); Member variables cannot be accessed by either method

Only use $this-> to access member variables

}

}

$stu = new Student; Creating objects

$stu->stu_id = ' 0607001 ';

$stu->stu_name = ' Xiao Li ';

$stu->sayname ();

Note: Properties defined in a class are not equivalent to global variables defined in the class and cannot be accessed directly from the method using property variables.

1. Construction and destruction:

PHP's opp mechanism, when new is complete, tries to invoke a method called __construct ().

If we write the initialized code to this method, we can complete the automatic initialization.

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

Public Function__construct ($id, $name)//Construction

{

$this->stu_id= $id;

$this->stu_name= $name;

}

}

$stu = new Student (' 100511101 ', ' Songyang ');

Note: If the constructor method has no parameters, then $stu = new Student and $stu = new Student () are all correct.

Compatibility issues with construction methods:

PHP5, the name of the construction method, is __construct (). Before php5, the constructor method has the same name as the class. For compatibility, a construction method with the same name as the class is also supported.

If both __construct () and the class name are constructed:

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

Public Function __construct ()

{

echo "construct run ...";

}

Public Function Student ()

{

Echo ' Student run ... ';

}

{

$stu = new Student;

Output: Construct run ...

Conclusion: If two construction methods are present, call __construct ()

Destruction:

A method is also automatically executed when the object is destroyed.

The destructor method name is: __destruct ();

Class Student

{

Public $stu _id;

Public $stu _name;

Public Function __construct ()

{

echo "Construct method call";

}

Destruction

Publicfunction __destruct ()

{

Freeing resources

echo "Destruction method call";

}

}

The method, which is used to release the extra resources that the object occupies, is not the memory space of the object itself!

In what case, the object will be destroyed:

1. The end of the script period, the object is automatically destroyed.

2. Destroy the variable that holds the object.

$stu = new Student;

Unset ($stu);

Output: Destructor method invocation

3. Save the variable for the object, and the other data is assigned.

$stu = new Student;

$stu = "New Value"; When other data is assigned, the student object is destroyed.

Assigning values between objects

The object supports reference passing, without the & symbol, so a new object cannot be obtained by means of the = assignment.

Example

Class Student

{

Public $stu _id;

Public $stu _name;

Public function __construct ($id, $name)

{

$this->stu_id= $id;

$this->stu_name= $name;

}

}

$stu 1 = new Student ("N", "song");

$stu 2 = $stu 1;

Echo $stu 1->stu_name;

Echo $stu 2->stu_name;

$stu 1->stu_name = "Songyang";

Echo $stu 1->stu_name;

Echo $stu 2->stu_name;

Output: Song song Songyang Songyang

Cloning

Use existing objects to get the same new object.

You need to use the keyword clone

New Object = Clone already has an object

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

Public function __construct ($id, $name)

{

$this->stu_id= $id;

$this->stu_name= $name;

}

}

$stu 1 = new Student ("N", "song");

$stu 2 = clone $stu 1;

Echo $stu 1->stu_name;

Echo $stu 2->stu_name;

$stu 1->stu_name = "Songyang";

Echo $stu 1->stu_name;

Echo $stu 2->stu_name;

Output: Song song Songyang song

When you clone an object, you need to modify some special properties of the object. means that some special processing needs to be done.

Used, when cloning, the method __clone () is automatically called to implement.

Automatically using the cloned object to invoke this __clone () method means that the method inside the $this represents the new object.

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

Public function __construct ($id, $name)

{

$this->stu_id= $id;

$this->stu_name= $name;

}

Public Function __clone ()

{

$this->stu_id= "0607002";

}

}

$stu 1 = new Student ("N", "Joker");

$stu 2 = clone $stu 1;

Echo $stu 1->stu_id;

Echo $stu 2->stu_id;

Output: 1000 0607002

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.