Php (7) object-oriented programming _ PHP Tutorial

Source: Internet
Author: User
Let's talk about php (7) object-oriented programming. Php (7) object-oriented programming I. class declaration and object initialization 1.1 when declaring a member attribute in the class: there must be a modifier before. if you do not know which one to use, use var. If php (7) Object-oriented programming

I. class declaration and object initialization

1.1 when declaring a member attribute in a class: there must be a modifier before. if you do not know which modifier to use, you can use var. if you know which modifier keyword to use, you will not use var.

Var $ color;

Var $ name = "zhangsan"


1.2 A file only saves one class. the file name contains the class name, for example, class name. class. php.

Person. class. php


1.3 use the new keyword to create an object. If an object is created, a space is allocated in the memory. $ object reference = new class name;

$ Person = new Person

 name;}}$p1 = new Person;   // Java: Person person = new Person;$p1->name = "lisi"; // Java: person.name = "lisi";$p1->say();         // Java: person.say();?>
1.4 allocate objects in memory

A. Stack memory: stores local variables

B. heap memory: storage objects

C. shared area: stores static variables.

D. code segment: storage method, etc.


2. constructor and Destructor

2.1 constructor:

A. constructor is the first method automatically called after the object is created.

B. in PHP4, the method with the same name as the class is the constructor.

C. in PHP5, use the magic method _ construct () as the constructor for Constructor. The constructor name is used for declaring constructor in all classes.

Advantage: The construction method does not need to be changed when the class name is changed.

D. Role of constructor: initialize member attributes

 Name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function say () {echo "my name: {$ this-> name}, my age: {$ this-> age}, my gender: {$ this-> sex }.
";}}$ P1 = new Person (" zhangsan ", 20," female "); $ p2 = new Person (" lisi ", 25 ); $ p3 = new Person ("wangwu"); $ p1-> say (); $ p2-> say (); $ p3-> say ();?>

2.2 destructor:

A. destructor refers to the last method automatically called before an object is released.

B. like Java, PHP also uses the garbage collector to release resources, but PHP recycles resources immediately after calling, while Java is not.

C. Role of the destructor: close some resources, perform some cleanup, and use the magic method _ destruct ()

 Name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function say () {echo "my name: {$ this-> name}, my age: {$ this-> age}, my gender: {$ this-> sex }.
";} Function _ destruct () {echo $ this-> name." Goodbye!
";}}$ P1 = new Person (" zhangsan ", 20," female "); $ p1-> say (); $ p1 = null; // my name: zhangsan, my age: 20, my Gender: Female. // Zhangsan goodbye!?>


2.3 magic methods

The magic method is a good method provided by the system. it is automatically called to complete a function at different times. different magic methods have different call times.

The magic method starts _

_ Construct (); // Constructor
_ Destruct (); // destructor
_ Set ();
_ Get ();
_ Isset ();
_ Unset ();
_ Clone ();
_ Call ();
_ Sleep ();
_ Weakup ();
_ ToString ()
_ Autoload ();

Objective (7) object-oriented programming I. class declaration and object initialization 1.1 when declaring a member attribute in the class: there must be a modifier before. if you do not know which one to use, use var. if...

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.