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

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

WBOY
发布: 2016-06-13 08:53:29
原创
985 人浏览过

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);
登录后复制

 

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板