PHP的静态方法与普通方法用法实例分析

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP的静态方法与普通方法用法。分享给大家供大家参考,具体如下:

代码

<?php
class TestClass {
    public $attributeone="one";
    public function TestFunctionOne(){
        echo "<br> testone";
    }
    public static $attributetwo="two";
    public static function TestFunctionTwo(){
        echo "<br> testtwo";
    }
}
$publicone=new TestClass();
// 非静态的属性
$attributeone=$publicone->attributeone;
print_r($attributeone);
// 非静态的方法
$publicone->TestFunctionOne();
// 静态不需要新建对象 直接可以使用类
// 静态的属性
$attributetwo=TestClass::$attributetwo;
print_r('<br>'.$attributetwo);
// 静态的方法
$attributetwo=TestClass::TestFunctionTwo();

对比

静态方法 :

1.从程序运行开始 就实例生成内存 ,所以可以直接调用,效率会高很多,但静态内存是有限制的,实例太多,程序会启动不了,2.静态内存会常驻  适用于多次频繁调用的

非静态方法:

1.实例方法开始生成内存,在调用时申请零散的内存,所以效率会慢很多 ,

2.非静态的用完就释放了  不会常驻

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

linux下删除7天前日志的代码(php+shell)

PHP版本: 复制代码 代码如下: /** * 删除7天前的日志 * @param $logPath */ function del7daysAgoLog($logPath) { if(...

PHP实现的消息实时推送功能【基于反ajax推送】

本文实例讲述了PHP实现的消息实时推送功能。分享给大家供大家参考,具体如下: 入口文件index.html <!DOCTYPE HTML> <html> &l...

PHP通用检测函数集合

// ※CheckMoney($C_Money) 检查数据是否是99999.99格式 // ※CheckEmailAddr($C_mailaddr) 判断是否为有效邮件地址 // ※Ch...

探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法

【造成原因】:Because PHP's integer type is signed, and many IP addresses will result in negative in...

PHP检测用户是否关闭浏览器的方法

本文实例讲述了PHP检测用户是否关闭浏览器的方法。分享给大家供大家参考,具体如下: 1、例子1 echo str_repeat(" ",3000); ignore_user_abor...