>백엔드 개발 >PHP 튜토리얼 >PHP学习日记(1)——类、函数的使用

PHP学习日记(1)——类、函数的使用

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-13 12:25:33832검색

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

一、自定义函数

<span style="color: #0000ff;">function</span> add(<span style="color: #800080;">$a</span>,<span style="color: #800080;">$b</span><span style="color: #000000;">){    </span><span style="color: #800080;">$c</span>=<span style="color: #800080;">$a</span>+<span style="color: #800080;">$b</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">echo</span> 'add test:'<span style="color: #000000;">;    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;}add(</span>1,2);

输出结果:

add test:3

 

二、调用类里面函数

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

<span style="color: #0000ff;">class</span><span style="color: #000000;"> test{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> add(<span style="color: #800080;">$a</span>,<span style="color: #800080;">$b</span><span style="color: #000000;">){        </span><span style="color: #800080;">$c</span>=<span style="color: #800080;">$a</span>+<span style="color: #800080;">$b</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">echo</span> 'class test:'<span style="color: #000000;">;            </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;    }}test</span>::add(1,2);

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

<span style="color: #0000ff;">class</span><span style="color: #000000;"> test{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> add(<span style="color: #800080;">$a</span>,<span style="color: #800080;">$b</span><span style="color: #000000;">){        </span><span style="color: #800080;">$c</span>=<span style="color: #800080;">$a</span>+<span style="color: #800080;">$b</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">echo</span> 'class test:'<span style="color: #000000;">;            </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;    }}</span><span style="color: #800080;">$object</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> test();</span><span style="color: #800080;">$object</span>->add(1,3);

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.