Home > Backend Development > PHP Tutorial > Learning classes and objects for PHP newbies (1)_PHP Tutorial

Learning classes and objects for PHP newbies (1)_PHP Tutorial

WBOY
Release: 2016-07-20 10:57:37
Original
757 people have browsed it

PHP 5 introduces a new object model. A complete rewrite of the way PHP handles objects, allowing for better performance and more features.

1. Basic concepts

1. class

The definition of each class starts with the keyword class, followed by Following the class name, it can be any name that is not a PHP reserved word. Followed by a pair of curly braces, which contains the definition of class members and methods. The pseudo variable $this can be used when a method is called inside the object. $this is a reference to the calling object (usually the object the method belongs to, but can also be another object if the method is called statically from within a second object). Look at the following example:

Example#1 $this variable in object-oriented language

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="keyword">class</span><span> A  </span></li><li class="alt"><span>{  </span></li><li><span class="keyword">function</span><span> foo()  </span></li><li class="alt"><span>{  </span></li><li><span class="keyword">if</span><span> (isset(</span><span class="vars">$this</span><span>)) {  </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">'$this is defined ('</span><span>;  </span></li><li><span class="func">echo</span><span> get_class(</span><span class="vars">$this</span><span>);  </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">")n"</span><span>;  </span></li><li><span>} </span><span class="keyword">else</span><span> {  </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">"$this is not defined.n"</span><span>;  </span></li><li><span>}  </span></li><li class="alt"><span>}  </span></li><li><span>}  </span></li><li class="alt"><span class="keyword">class</span><span> B  </span></li><li><span>{  </span></li><li class="alt"><span class="keyword">function</span><span> bar()  </span></li><li><span>{  </span></li><li class="alt"><span>A::foo();  </span></li><li><span>}  </span></li><li class="alt"><span>}  </span></li><li><span class="vars">$a</span><span> = </span><span class="keyword">new</span><span> A();  </span></li><li class="alt"><span class="vars">$a</span><span>->foo();  </span></span></li>
<li><span>A::foo();  </span></li>
<li class="alt">
<span class="vars">$b</span><span> = </span><span class="keyword">new</span><span> B();  </span>
</li>
<li>
<span class="vars">$b</span><span>->bar();  </span>
</li>
<li class="alt"><span>B::bar();  </span></li>
<li><span>?> </span></li>
</ol>
Copy after login

The above example will output:

<ol class="dp-xml">
<li class="alt"><span><span>$this is defined (a)  </span></span></li>
<li><span>$this is not defined.  </span></li>
<li class="alt"><span>$this is defined (b)  </span></li>
<li><span>$this is not defined. </span></li>
</ol>
Copy after login

Example#2 Simple class definition

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="keyword">class</span><span> SimpleClass  </span></li><li class="alt"><span>{  </span></li><li><span class="comment">// 成员声明 </span><span> </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="vars">$var</span><span> = </span><span class="string">'a default value'</span><span>;  </span></li><li><span class="comment">// 方法声明 </span><span> </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> displayVar() {  </span></li><li><span class="func">echo</span><span> </span><span class="vars">$this</span><span>-></span><span class="keyword">var</span><span>;  </span></span></li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

Example#3 Default value of class members

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li><span class="keyword">class</span><span> SimpleClass  </span></li><li class="alt"><span>{  </span></li><li><span class="comment">// 无效的类成员定义: </span><span> </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="vars">$var1</span><span> = </span><span class="string">'hello '</span><span>.</span><span class="string">'world'</span><span>;  </span></li><li><span class="keyword">public</span><span> </span><span class="vars">$var2</span><span> = <<<EOD  </span></li><li class="alt"><span>hello world  </span></li><li><span>EOD;  </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="vars">$var3</span><span> = 1+2;  </span></li><li><span class="keyword">public</span><span> </span><span class="vars">$var4</span><span> = self::myStaticMethod();  </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="vars">$var5</span><span> = </span><span class="vars">$myVar</span><span>;  </span></li><li><span class="comment">// 正确的类成员定义: </span><span> </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="vars">$var6</span><span> = myConstant;  </span></li><li><span class="keyword">public</span><span> </span><span class="vars">$var7</span><span> = self::classConstant;  </span></li><li class="alt"><span class="keyword">public</span><span> </span><span class="vars">$var8</span><span> = </span><span class="keyword">array</span><span>(true, false);  </span></li><li><span>}  </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

2, new

To create an instance of an object, you must create a new object and assign it to a variable. A new object is always assigned a value when it is created, unless the object defines a constructor and an exception is thrown on error.

Example#4 Create an instance

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$instance</span><span> = </span><span class="keyword">new</span><span> SimpleClass();  </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

Copy code When assigning an already created instance of an object to a new variable, the new variable will access the same instance, just like using the Object assignment is the same. This behavior is the same as when passing an instance to a function. You can use cloning to create a new instance of an already created object.

Example#5 Object assignment

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$assigned</span><span>   =  </span><span class="vars">$instance</span><span>;  </span></li><li class="alt"><span class="vars">$reference</span><span>  =& </span><span class="vars">$instance</span><span>;  </span></li><li><span class="vars">$instance</span><span>-></span><span class="keyword">var</span><span> = </span><span class="string">'$assigned will have this value'</span><span>;  </span></span></li>
<li class="alt">
<span class="vars">$instance</span><span> = null; </span><span class="comment">// $instance and $reference become null </span><span> </span>
</li>
<li>
<span>var_dump(</span><span class="vars">$instance</span><span>);  </span>
</li>
<li class="alt">
<span>var_dump(</span><span class="vars">$reference</span><span>);  </span>
</li>
<li>
<span>var_dump(</span><span class="vars">$assigned</span><span>);  </span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

Copying the code above will output:

<ol class="dp-xml">
<li class="alt"><span><span>NULL  </span></span></li>
<li><span>NULL  </span></li>
<li class="alt"><span>object(SimpleClass)#1 (1) {  </span></li>
<li>
<span>["var"]=</span><span class="tag">></span><span> </span>
</li>
<li class="alt"><span>string(30) "$assigned will have this value"  </span></li>
<li><span>} </span></li>
</ol>
Copy after login

3. extends

A class can inherit the methods and members of another class using the extends keyword in the declaration. You cannot extend multiple classes, you can only inherit one base class.

Inherited methods and members can be overridden by redeclaring them with the same name, unless the parent class uses the final keyword when defining the method. Overridden methods or members can be accessed through parent::.

Example#6 Simple class inheritance

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="keyword">class</span><span> ExtendClass </span><span class="keyword">extends</span><span> SimpleClass  </span></li><li class="alt"><span>{  </span></li><li><span class="comment">// Redefine the parent method </span><span> </span></li><li class="alt"><span class="keyword">function</span><span> displayVar()  </span></li><li><span>{  </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">"Extending classn"</span><span>;  </span></li><li><span>parent::displayVar();  </span></li><li class="alt"><span>}  </span></li><li><span>}  </span></li><li class="alt"><span class="vars">$extended</span><span> = </span><span class="keyword">new</span><span> ExtendClass();  </span></li><li><span class="vars">$extended</span><span>->displayVar();  </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

The above example will output:

<ol class="dp-xml">
<li class="alt"><span><span>Extending class  </span></span></li>
<li><span>a default value </span></li>
</ol>
Copy after login

1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445764.htmlTechArticlePHP 5 introduces a new object model (Object Model). A complete rewrite of the way PHP handles objects, allowing for better performance and more features. 1. Basic concepts 1. class The definition of each class...
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