Home  >  Article  >  Backend Development  >  php-php进阶 - php如何写一个简单的类的拓展?

php-php进阶 - php如何写一个简单的类的拓展?

WBOY
WBOYOriginal
2016-06-06 20:12:461115browse

目前只会写一个函数,我想写个简单的类。

回复内容:

目前只会写一个函数,我想写个简单的类。

<code><?php /**
 * 我是个类
 */
class person
{    
    /**
     * 我的手
     */
    private $_hand = 'myhand';

    /**
     * 我会吃饭
     */
    public function eat($food)
    {
        echo 'i eat'.$food;
    }
}



</code></code>

<code><?php class 我是加法

{
    
    function __construct($a,$b)
    {
    $this->a=$a;
    $this->b=$b;
    }



    function __toString()
    {
        $c=$this->a+$this->b;
        $c=strval($c);
        return $c  ;
    }



}


echo new 我是加法(1,2);</code>
Statement:
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