首頁 >php教程 >php手册 >PHP学习日记(一)类、函数的使用,php日记

PHP学习日记(一)类、函数的使用,php日记

WBOY
WBOY原創
2016-06-13 08:53:29974瀏覽

PHP学习日记(一)——类、函数的使用,php日记

一、自定义函数

<span>function</span> add(<span>$a</span>,<span>$b</span><span>){
    </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
    </span><span>echo</span> 'add test:'<span>;
    </span><span>echo</span> <span>$c</span><span>;
    </span><span>return</span> <span>$c</span><span>;
}
add(</span>1,2);

输出结果:

add test:3

 

二、调用类里面函数

 1、双冒号::,不用实例化,直接类名调用

<span>class</span><span> test{
    </span><span>public</span> <span>function</span> add(<span>$a</span>,<span>$b</span><span>){
        </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
        </span><span>echo</span> 'class test:'<span>;    
        </span><span>echo</span> <span>$c</span><span>;        
        </span><span>return</span> <span>$c</span><span>;
    }
}
test</span>::add(1,2);

2、->,实例化后的对象使用

<span>class</span><span> test{
    </span><span>public</span> <span>function</span> add(<span>$a</span>,<span>$b</span><span>){
        </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
        </span><span>echo</span> 'class test:'<span>;    
        </span><span>echo</span> <span>$c</span><span>;        
        </span><span>return</span> <span>$c</span><span>;
    }
}
</span><span>$object</span>=<span>new</span><span> test();
</span><span>$object</span>->add(1,3);

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn