Access and construction destructor of members in PHP object-oriented 13 objects

WBOY
Release: 2016-07-29 09:12:40
Original
871 people have browsed it

1.1 Access to members in the object

<code>对成员属性的访问包括赋值操作和获取成员属性的操作。访问对象中的成员和访问数组中的元素类似。只能通过对象的引用来访问对象中的每个成员。
    $引用名 = new 类名称([参数数列表]); //对象实例化格式
    $引用名 -> 成员属性 = 值;  //对成员属性赋值的操作
    $echo $引用名 -> 成员属性; //获取成员属性的值
    $引用名 -> 成员方法; //访问对象中的成员方法
</code>
Copy after login

If the members in the object are not static, then "object reference name->property" or "object reference name->method" is the only access form.

1.2 Special object reference "$this"

<code>    不管是在对象的外部还是在对象的内部,访问对象中的成员都必须使用对象的引用变量。但对象创建完成以后,对象的引用名称无法在对象的方法中找到。如果在对象的方法中再使用new关键字创建一个对象则是另一个对象,调用的成员也是另一个新创建对象的成员。
    对象一旦被创建,在对象中的每个成员方法里面都会存在一个特殊的对象引用“$this”。成员方法属于哪个对象,$this引用就代表哪个对象,专门用来完成对象内部成员之间的访问。
    不管是在对象内部使用$this访问自己对象内部的成员,还是在对象外部通过对象的引用名称访问对象中的成员。都需要使用特殊的运算符"->"
</code>
Copy after login
<code><span><span>class</span><span>Person</span>{</span><span>var</span><span>$name</span>;
    <span>var</span><span>$sex</span>;
    <span>var</span><span>$age</span>;
    <span><span>function</span><span>say</span><span>()</span>{</span><span>echo</span><span>"我的名字:"</span>.<span>$this</span>->name.<span>" 性别:"</span>.<span>$this</span>->sex.<span>" 年龄"</span>.<span>$this</span>->age;
    }
    <span><span>function</span><span>run</span><span>()</span>{</span><span>echo</span><span>$this</span>->name.<span>"在走路<br>"</span>;
    }
}</code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the access, construction and destructor of members in PHP object-oriented 13 objects, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!