<span id="Label3"></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"><strong>Declaration and object initialization of a class</strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">1.1 When declaring a member property in a class: there must be a modifier in front of it, use <span style="font-family:‘Comic Sans MS‘; font-size:14px; line-height:26px"></span> var when you don't know what to use, and if you know the Modifier keyword <span style="font-family:‘Comic Sans MS‘; font-size:14px; line-height:26px">,</span> you don't use var.</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">var $color;<br></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">var $name = "zhangsan"</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">1.2 A file holds only one class <span style="font-family:‘Comic Sans MS‘; font-size:14px; line-height:26px">, and</span> the file name contains the class name <span style="font-family:‘Comic Sans MS‘; font-size:14px; line-height:26px">, such as</span> : Class Name. class.php</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">person.class.php</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><br></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">1.3 Create an object with the new keyword, create an object to allocate a space in memory $ object reference = new Class name;</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">$person = new Person</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS"></span></p></p><pre code_snippet_id="447325" snippet_file_name="blog_20140810_1_8140551" name="code" class="java"><pre code_snippet_id="447325" snippet_file_name="blog_20140810_1_8140551" name="code" class="java"><?phpclass person {var $name; Java:private String Name;var $age; var $sex; function say () {echo $this->name;}} $p 1 = new person; Java:person person = new person, $p 1->name = "lisi"; Java:person.name = "lisi"; $p 1->say (); Java:person.say ();? ></pre></pre><span style="font-family:‘Comic Sans MS‘"><span style="font-family:‘Comic Sans MS‘">1.4</span></span><span style="font-family:‘Comic Sans MS‘">the <span style="font-family:‘Comic Sans MS‘">allocation of objects in memory</span></span><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">A. Stack memory: store local variables</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">B. Heap memory: storing objects</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">C. Shared area: storing static variables</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"><span style="font-family:Comic Sans MS">D. Code snippet: storage method, etc.</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"><strong>second, the constructor function and the destructor</strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">2.1 Constructors:</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">A. Construction method is the first method that is automatically called after object creation is completed</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">B. in PHP4, the method with the same name as the class is the construction method</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">C. In PHP5, the method of construction is chosen using the Magic method __construct (), which is used in all classes to declare the constructor method using this name</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">Pros: the construction method does not change when changing the class name</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">D. How the constructor Works: class for member properties</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"></span></p></p><pre code_snippet_id="447325" snippet_file_name="blog_20140810_2_9853201" name="code" class="php"><pre code_snippet_id="447325" snippet_file_name="blog_20140810_2_9853201" name="code" class="php"><?phpclass person {var $name, var $age, var $sex, function __construct ($name = "", $age =0, $sex = "male") {$this->name=$ Name: $this->age= $age; $this->sex= $sex;} Function say () {echo ' My name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}. <br> ";}} $p 1=new person ("zhangsan", 20, "female"), $p 2=new person ("lisi"), $p 3=new person ("wangwu"); $p 1->say (); $p 2->say (); $p 3->say ();? ></pre></pre><br><span style="font-family:‘Comic Sans MS‘"><span style="font-family:‘Comic Sans MS‘">2.2 destructors:</span></span><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px; font-family:Arial"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px; font-family:Arial"><span style="font-family:‘Comic Sans MS‘">A. A destructor is the last method that is called automatically before the object is released</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px; font-family:Arial"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px; font-family:Arial"><span style="font-family:‘Comic Sans MS‘">B. Like java, PHP frees up resources using the garbage collector, except that it is recycled immediately after PHP calls, while Java is Not.</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px; font-family:Arial"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px; font-family:Arial"><span style="font-family:‘Comic Sans MS‘">C. Function of Destructors: Close some resources, do some cleanup work, use magic method __destruct ()</span></p></p><pre code_snippet_id="447325" snippet_file_name="blog_20140810_3_9961885" name="code" class="php"><pre code_snippet_id="447325" snippet_file_name="blog_20140810_3_9961885" name="code" class="php"><?phpclass person {var $name, var $age, var $sex, function __construct ($name = "", $age =0, $sex = "male") {$this->name=$ Name: $this->age= $age; $this->sex= $sex;} Function say () {echo ' My name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}. <br> ";} function __destruct () {echo $this->name. " Good bye! <br> ";}} $p 1=new person ("zhangsan", 20, "female"), $p 1->say (); $p 1 = null;//my name: zhangsan, my age: 20, my sex: female. ///zhangsan goodbye!?></pre></pre><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘"><br></span></p></p>2.3 Magic Methods<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">The Magic method is the system gives us a good, at different times to complete a function and automatic call method, different magic methods have different call timing</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">Magic method begins with __</span></p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:‘Comic Sans MS‘">__construct (); constructor function<br>__destruct (); Destructors<br>__set ();<br>__get ();<br>__isset ();<br>__unset ();<br>__clone ();<br>__call ();<br>__sleep ();<br>__weakup ();<br>__tostring ()<br>__autoload ();<br><br></span></p></p></span>
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