Home > Backend Development > PHP Tutorial > PHP object-oriented key points [translated]

PHP object-oriented key points [translated]

WBOY
Release: 2016-07-30 13:31:21
Original
822 people have browsed it

  • __construct
<code><span><span><?php</span><span><span>class</span><span>ConstructTest</span> {</span><span>private</span><span>$arg1</span>;
    <span>private</span><span>$arg2</span>;

    <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$arg1</span>, <span>$arg2</span>)</span> {</span><span>$this</span>->arg1 = <span>$arg1</span>;
        <span>$this</span>->arg2 = <span>$arg2</span>;
        <span>print</span><span>"__construct is called...\n"</span>;
    }
    <span>public</span><span><span>function</span><span>printAttributes</span><span>()</span> {</span><span>print</span><span>'$arg1 = '</span>.<span>$this</span>->arg1.<span>' $arg2 = '</span>.<span>$this</span>->arg2.<span>"\n"</span>;
    }
}
<span>$testObject</span> = <span>new</span> ConstructTest(<span>"arg1"</span>,<span>"arg2"</span>); 
<span>$testObject</span>->printAttributes();</span></span></code>
Copy after login

The running result is as follows:

<code><span>Stephens</span>-<span>Air</span><span>:Desktop</span><span>$ </span>php <span>Test</span>.php 
__construct is called...
<span>$arg1</span> = arg1 <span>$arg2</span> = arg2</code>
Copy after login
  • parent

is used to directly call the method in the parent class in the subclass

<code><span><span><?php</span><span><span>class</span><span>BaseClass</span> {</span><span>protected</span><span>$arg1</span>;
    <span>protected</span><span>$arg2</span>;

    <span><span>function</span><span>__construct</span><span>(<span>$arg1</span>, <span>$arg2</span>)</span> {</span><span>$this</span>->arg1 = <span>$arg1</span>;
        <span>$this</span>->arg2 = <span>$arg2</span>;
        <span>print</span><span>"__construct is called...\n"</span>;
    }
    <span><span>function</span><span>getAttributes</span><span>()</span> {</span><span>return</span><span>'$arg1 = '</span>.<span>$this</span>->arg1.<span>' $arg2 = '</span>.<span>$this</span>->arg2;
    }
}

<span><span>class</span><span>SubClass</span><span>extends</span><span>BaseClass</span> {</span><span>protected</span><span>$arg3</span>;

    <span><span>function</span><span>__construct</span><span>(<span>$baseArg1</span>, <span>$baseArg2</span>, <span>$subArg3</span>)</span> {</span><span>parent</span>::__construct(<span>$baseArg1</span>, <span>$baseArg2</span>);
        <span>$this</span>->arg3 = <span>$subArg3</span>;
    }
    <span><span>function</span><span>getAttributes</span><span>()</span> {</span><span>return</span><span>parent</span>::getAttributes().<span>' $arg3 = '</span>.<span>$this</span>->arg3;
    }
}
<span>$testObject</span> = <span>new</span> SubClass(<span>"arg1"</span>,<span>"arg2"</span>,<span>"arg3"</span>); 
<span>print</span><span>$testObject</span>->getAttributes().<span>"\n"</span>;</span></span></code>
Copy after login

The running result is as follows:

<code><span>Stephens</span>-<span>Air</span><span>:Desktop</span><span>$ </span>php <span>Test</span>.php 
__construct is called...
<span>$arg1</span> = arg1 <span>$arg2</span> = arg2 <span>$arg3</span> = arg3</code>
Copy after login
  • self
    The prefix modification for calling static members and static methods of the class within a class, and this for non-static member variables and functions.
<code><span><span><?php</span><span><span>class</span><span>StaticExample</span> {</span><span>static</span><span>public</span><span>$arg1</span> = <span>"Hello, This is static field.\n"</span>;
    <span>static</span><span>public</span><span><span>function</span><span>sayHello</span><span>()</span> {</span><span>print</span><span>self</span>::<span>$arg1</span>;
    }
}

<span>print</span> StaticExample::<span>$arg1</span>;
StaticExample::sayHello();</span></code>
Copy after login
  • static
    The static keyword introduced here is mainly used for the new delayed static binding function of PHP 5.3 and above.
<code><span><span><?php</span><span>abstract</span><span><span>class</span><span>Base</span> {</span><span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>//这里的new static()实例化的是调用该静态方法的当前类。</span><span>return</span><span>new</span><span>static</span>();
    }
    <span>abstract</span><span>public</span><span><span>function</span><span>printSelf</span><span>()</span>;</span>}

<span><span>class</span><span>SubA</span><span>extends</span><span>Base</span> {</span><span>public</span><span><span>function</span><span>printSelf</span><span>()</span> {</span><span>print</span><span>"This is SubA::printSelf.\n"</span>;
    }
}

<span><span>class</span><span>SubB</span><span>extends</span><span>Base</span> {</span><span>public</span><span><span>function</span><span>printSelf</span><span>()</span> {</span><span>print</span><span>"This is SubB::printSelf.\n"</span>;
    }
}

SubA::getInstance()->printSelf();
SubB::getInstance()->printSelf();</span></span></code>
Copy after login

The running results are as follows:

<code>Stephens-Air:Desktop$ php Test.php 
This <span>is</span> SubA<span>::printSelf.</span>
This <span>is</span> SubB<span>::printSelf.</span></code>
Copy after login

The static keyword can not only be used for instantiation. Like self and parent, static can also be used as an identifier for static method calls, even from non-static contexts. In this scenario, self still represents the class where the current method is located. See the following code:

<code><span><span><?php</span><span>abstract</span><span><span>class</span><span>Base</span> {</span><span>private</span><span>$ownedGroup</span>;
    <span>public</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>//这里的static和上面的例子一样,表示当前调用该方法的实际类。</span><span>//需要另外说明的是,这里的getGroup方法即便不是静态方法,也会得到相同的结果。然而倘若</span><span>//getGroup真的只是普通类方法,那么这里还是建议使用$this。</span><span>$this</span>->ownedGroup = <span>static</span>::getGroup();
    }
    <span>public</span><span><span>function</span><span>printGroup</span><span>()</span> {</span><span>print</span><span>"My Group is "</span>.<span>$this</span>->ownedGroup.<span>"\n"</span>;
    }
    <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>return</span><span>new</span><span>static</span>();
    }
    <span>public</span><span>static</span><span><span>function</span><span>getGroup</span><span>()</span> {</span><span>return</span><span>"default"</span>;
    }
}

<span><span>class</span><span>SubA</span><span>extends</span><span>Base</span> {</span>
}

<span><span>class</span><span>SubB</span><span>extends</span><span>Base</span> {</span><span>public</span><span>static</span><span><span>function</span><span>getGroup</span><span>()</span> {</span><span>return</span><span>"SubB"</span>;
    }
}

SubA::getInstance()->printGroup();
SubB::getInstance()->printGroup(); </span></span></code>
Copy after login

The running result is as follows:

<code>
Stephens-Air:Desktop$ php Test.php 
My <span>Group</span><span>is</span><span>default</span>
My <span>Group</span><span>is</span> SubB</code>
Copy after login
  • __destruct
    The role of the destructor method is exactly the opposite of the construction method __construct. It is only automatically called before the object is collected by the garbage collector. We can use this method to do some necessary cleanup work.
<code><span><span><?php</span><span><span>class</span><span>TestClass</span> {</span><span><span>function</span><span>__destruct</span><span>()</span> {</span><span>print</span><span>"TestClass destructor is called.\n"</span>;
    }
}

<span>$testObj</span> = <span>new</span> TestClass();
<span>unset</span>(<span>$testObj</span>);
<span>print</span><span>"Application will exit.\n"</span>;</span></code>
Copy after login

The running results are as follows:

<code>
Stephens-Air:Desktop$ php Test.php 
TestClass <span><span>destructor</span><span>is</span><span>called</span>.
<span>Application</span><span>will</span><span>exit</span>.</span></code>
Copy after login
  • __clone
    In versions after PHP 5, assignment between objects is reference assignment, that is, the two objects after assignment will point to the same address space. If you want to assign values ​​based on objects, you can use the clone method provided by PHP. This method returns a copy of the current object after a shallow copy. If you want to complete some special operations during the clone process, such as deep copy, you need to implement the __clone method in the declaration of the current class. This method is executed during the clone process. will be called implicitly. In addition, special attention should be paid to the fact that the __clone method is executed on the object being copied, that is, the object after assignment.
<code><span><span><?php</span><span><span>class</span><span>InnerClass</span> {</span><span>public</span><span>$id</span> = <span>10</span>;
    <span>public</span><span><span>function</span><span>printSelf</span><span>()</span> {</span><span>print</span><span>'$id = '</span>.<span>$this</span>->id.<span>"\n"</span>;
    }
}

<span><span>class</span><span>OuterClass</span> {</span><span>public</span><span>$innerClass</span>;
    <span>public</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>$this</span>->innerClass = <span>new</span> InnerClass();
    }
    <span>public</span><span><span>function</span><span>__clone</span><span>()</span> {</span><span>$this</span>->innerClass = <span>clone</span><span>$this</span>->innerClass;
        <span>print</span><span>"__clone is called.\n"</span>;
    }
}

<span>$outerA</span> = <span>new</span> OuterClass();
<span>print</span><span>"Before calling to clone.\n"</span>;
<span>$outerB</span> = <span>clone</span><span>$outerA</span>;
<span>print</span><span>"After calling to clone.\n"</span>;
<span>$outerA</span>->innerClass->id = <span>20</span>;
<span>print</span><span>"In outerA: "</span>;
<span>$outerA</span>->innerClass->printSelf();
<span>print</span><span>"In outerB: "</span>;
<span>$outerB</span>->innerClass->printSelf();</span></span></code>
Copy after login

The running results are as follows:

<code>
Stephens<span>-Air</span>:Desktop$ php Test<span>.</span>php 
Before calling <span>to</span> clone<span>.</span>
__clone is called<span>.</span>
After calling <span>to</span> clone<span>.</span><span>In</span> outerA: <span>$id</span><span>=</span><span>20</span><span>In</span> outerB: <span>$id</span><span>=</span><span>10</span></code>
Copy after login
  • const
    PHP5可以在类中定义常量属性。和全局常量一样,一旦定义就不能改变。常量属性不需要像普通属性那样以访

    以上就介绍了php面向对象要点[转],包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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