Php Object-Oriented Learning notes? Structure, structure, object assignment, clone-php Tutorial

Source: Internet
Author: User
Php Object-Oriented Learning notes? Constructor, destructor, object assignment, clone Php Object-Oriented Learning notes? Structure, structure, object assignment, and clone

Class student

{

Public $ stu_id; // define the member variable

Public $ stu_name;

Public function sayName () // member function

{

Echo $ this-> stu_name; // $ this accesses the member variable of this object

}

}

$ Stu = new Student; // create an object

$ Stu-> stu_id = '123 ';

$ Stu-> stu_name = 'Lily ';

$ Stu-> sayName ();

Note:

Class student

{

Public $ stu_id; // define the member variable

Public $ stu_name;

Public function sayName () // member function

{

Var_dump ($ stu_name );

Var_dump ($ GLOBALS ['Stu _ name']); // both methods cannot access member variables.

// Only $ this-> can be used to access member variables

}

}

$ Stu = new Student; // create an object

$ Stu-> stu_id = '123 ';

$ Stu-> stu_name = 'Lily ';

$ Stu-> sayName ();

Note: The attributes defined in the class are not equivalent to the global variables defined in the class, and cannot be accessed directly in the form of attribute variables in the method.

1. Structure and structure:

Php's opp mechanism will try to call a method called _ construct () when new is complete.

If we write the initialization code into this method, automatic initialization can be completed.

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public function _ construct ($ id, $ name) // construct

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

}

}

$ Stu = new Student ('123', 'songyang ');

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

Constructor compatibility:

Php5, the constructor name, is _ construct (). before php5, the constructor name is the same as the class name. For compatibility, constructor with the same name as the class is also supported.

If the constructor _ construct () and class name appear simultaneously:

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 constructor methods exist at the same time, call _ construct ()

Analysis structure:

When an object is destroyed, a method is automatically executed.

The Destructor name is __destruct ();

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public function _ construct ()

{

Echo "constructor call ";

}

// Structure

Publicfunction _ destruct ()

{

// Release resources

Echo "destructor call ";

}

}

This method is used to release the object. the additional resources occupied by this object are not the memory space of the object!

Under what circumstances will the object be destroyed:

1. after the script cycle ends, the object is automatically destroyed.

2. destroy the variables that save the object.

$ Stu = new Student;

Unset ($ stu );

Output: destructor call

3. the variable that saves the object is assigned another value.

$ Stu = new Student;

$ Stu = "new Value"; // when values are assigned to other data, the Student object is destroyed.

Assignment between objects

Objects can be referenced and transmitted without an ampersand. Therefore, a new object cannot be obtained in the form of = value assignment.

Example

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public function _ construct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

}

}

$ Stu1 = new Student ("100", "song ");

$ Stu2 = $ stu1;

Echo $ stu1-> stu_name;

Echo $ stu2-> stu_name;

$ Stu1-> stu_name = "songyang ";

Echo $ stu1-> stu_name;

Echo $ stu2-> stu_name;

Output: song songyang

Clone

Use existing objects to obtain the same new object.

Keyword clone is required.

New object = clone existing object

Example:

Class Student

{

Public $ stu_id;

Public $ stu_name;

Public function _ construct ($ id, $ name)

{

$ This-> stu_id = $ id;

$ This-> stu_name = $ name;

}

}

$ Stu1 = new Student ("100", "song ");

$ Stu2 = clone $ stu1;

Echo $ stu1-> stu_name;

Echo $ stu2-> stu_name;

$ Stu1-> stu_name = "songyang ";

Echo $ stu1-> stu_name;

Echo $ stu2-> stu_name;

Output: song songyang song

When cloning an object, you must modify some special attributes of the object. This means that some special processing is required.

When cloning, the system automatically calls the _ clone () method.

The clone object is automatically used to call the _ clone () method, which means that $ this inside the method 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 ";

}

}

$ Stu1 = new Student ("1000", "joker ");

$ Stu2 = clone $ stu1;

Echo $ stu1-> stu_id;

Echo $ stu2-> stu_id;

Output: 1000 0607002

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.