在php中静态方法我们就直接在函数或变量前加一个static就可以了,使用的时候和静态变量差不多,不需要实例化,直接用::调用了,下面我来给大家举几个关于静态方法实例。
PHP也不例外!所谓静态方法(属性)就是以static关键词标注的属性或者方法(例如:静态属性public static username;)
静态方法和非静态方法最大的区别在于他们的生命周期不同,用一个实例来说明静态方法定义,定义静态方法很简单,在声明关键词function之前加上static,实例代码如下:
- class A
- {
- static function fun()
- {
- // do somathing
- }
静态方法使用
使用的时候和静态变量差不多,不需要实例化,直接用::调用,实例代码如下
A::fun()
对比普通方法
因为静态方法的调用不需要实例化,所以在静态方法中引用类自身的属性或者方法的时候会出错,也就是形如self和$this是错误的,实例代码如下:
- class MyClass
- {
- public $num = 5;
- function __construct()
- {
- $this->num = 10;
- }
- function fun_1()
- {
- echo "I am a public method named fun_1.n";
- echo "The num of object is {$this->num}.n";
- }
- static function fun_2()
- {
- echo "I am a static method named fun_2.n";
- }
- function fun_3($n)
- {
- echo "The arg is {$n}n";
- }
- }
- $m = new MyClass;
- $m->fun_1();
- $m->fun_2();
- $m->fun_3('test');
- MyClass::fun_1();
- MyClass::fun_2();
- MyClass::fun_3('test');
- /*
- 输出结果:
- lch@localhost:php $ php class_method.php
- I am a public method named fun_1.
- The num of object is 10.
- I am a static method named fun_2.
- The arg is test
- I am a public method named fun_1.
- PHP Fatal error: Using $this when not in object context in /Users/lch/program/php/class_method.php on line 14
- */
再看一实例,用一个实例来说明,代码如下:
- class user{
- public static $username; //声明一个静态属性
- public $password; //声明一个非静态属性
- function __construct($pwd) {
- echo ‘Username:’,self::$username; //输出静态属性
- self::$username = ‘admin’; //为静态属性赋值
- $this->password = $pwd; //为非静态属性赋值
- }
- public function show(){ //输出类属性
- echo ‘
- Username:’,self::$username;
- echo ‘
- Password:’,$this->password;
- }
- public static function sshow(){
- echo ‘
- Username:’,self::$username;
- echo ‘
- Password:’,$this->password;
- }
- }
- user::$username = ‘root’; //为赋值user类的静态属性赋值
- $objUser = new user(’123456′); //实例化user类
- $objUser->sshow();
- unset($objUser);
- echo ‘
- Username:’,user::$username;
- /*
- * 输出结果为:
- * Username:root
- * Username:admin
- * Password:123456
- * Usern
- ame:admin
- * */
从这里实例中可以看出,静态属性在类实例化以前就起作用了,并且在对象被销毁时静态属性依然可以发挥作用!
也因为静态方法的这种属性,所以不能在静态方法中调用非静态属性或者方法
接着看
1、php类中,假设所有的属性与方法的可见性为public,那么在外部访问类的方法或属性时,都必须通过对象【类的实例化过程】来调用,实例代码如下:
- class Log
- {
- public $root = DIRECTORY_SEPARATOR;
- public $logPath = '/data/app/www/test-realtime.monitoring.c.kunlun.com/log';
- public $defaultDir = 'default';
- public function writeLog($logName, $logType, $data, $newDir = FALSE)
- {
- $fileName = '';
- if (!file_exists($this->logPath))
- {
- mkdir($this->logPath, 0777);
- }
- if ($newDir !== FALSE)
- {
- @mkdir($this->logPath.$this->root.$newDir, 0777);
- $fileName = $this->logPath.$this->root.$newDir.$this->root.date('Y-m-d', time()).'_'.$logName.'_'.$logType.'.log';
- }
- else
- {
- @mkdir($this->logPath.$this->root.$this->defaultDir, 0777);
- $fileName = $this->logPath.$this->root.$this->defaultDir.$this->root.date('Y-m-d', time()).'_'.$logName.'_'.$logType.'.log';
- }
- file_put_contents($fileName, date('Y-m-d H:i:s').' '.$data."n", FILE_APPEND);
- }
- }
类的实例化对象的过程:$logObj = new Log();
访问类中的方法:$logObj->writeLog($param1, $param2, $param3, $param4);
访问类中的属性:echo $logObj->root;
2、如果类中的属性前被static关键字修饰时,就不能通过对象来访问被static修饰的属性,但如果是类中的方法被static修饰时则即可以通过对象也可以通过类名::方法名的方式来进行访问。
3、如果类中的方法被static修饰则,方法中不能用$this,$this指的是类的实例化对象,由于静态方法不用通过对象就可以调用,所以伪变量$this不可用。
波比源码 » PHP类中的静态方法使用实例
purchase levaquin online brand levofloxacin 500mg
dutasteride online order order celecoxib 200mg without prescription buy zofran 4mg online cheap
brand spironolactone 25mg buy finasteride without prescription fluconazole 200mg ca
purchase sildenafil generic purchase estradiol online estrace cheap
buy tadalis 10mg online cheap voltaren 100mg pill diclofenac ca
isotretinoin order online buy accutane online zithromax 250mg tablet
order prednisone 20mg for sale order prednisone 20mg cheap sildenafil 100mg
order provigil 200mg pill buy acetazolamide 250 mg generic buy diamox pills
purchase altace pills order clobetasol generic azelastine 10 ml without prescription
buspirone price buy cordarone 200mg online generic ditropan 5mg
order alendronate 70mg online cheap purchase ibuprofen sale buy pepcid for sale
order imdur 40mg pills digoxin 2250 mg pills order telmisartan 80mg pills
buy molnunat 200 mg generic buy prevacid 30mg lansoprazole usa
sertraline ca order sertraline 100mg pill usa viagra sales
salbutamol 100 mcg cheap viagra 25 mg sildenafil 100mg cheap
order cialis 5mg online order tadalafil 40mg pill viagra for women
order provera 10mg online microzide 25mg for sale cyproheptadine 4mg usa
accutane for sale online cheap amoxil 1000mg deltasone 10mg tablet
order piracetam online cheap purchase sildenafil online sildenafil price
buy tadalafil 5mg sale oral cialis 10mg viagra cost
order generic furosemide doxycycline 100mg canada plaquenil pills
oral aralen chloroquine online baricitinib sale
purchase sporanox pills oral tindamax 300mg tinidazole brand
clozaril 100mg over the counter order decadron 0,0,5 mg for sale dexamethasone price
sildenafil 50mg for sale lisinopril 5mg canada purchase lisinopril generic
zyvox 600 mg over the counter purchase linezolid generic real money slots
pay for research papers online roulette wheel real casino slots
brand cialis 40mg viagra 150mg buy sildenafil 100mg for sale
cialis 5mg usa viagra 50mg over the counter buy sildenafil 50mg generic
orlistat brand xenical 60mg for sale order zovirax 400mg sale
tadalafil for sale buy cialis 40mg sale purchase plavix generic
order zyloprim 100mg online purchase crestor generic ezetimibe usa
order domperidone 10mg without prescription tetracycline 250mg ca order flexeril 15mg generic
lioresal cost tizanidine 2mg ca toradol canada
buy imitrex buy levofloxacin 250mg generic order avodart 0.5mg
order generic colchicine colchicine order online online roulette game real money
free roulette online online casino no deposit bonus best casino games
oral cialis 40mg order cialis online cheap brand name viagra
purchase albuterol online cheap albuterol buy online cipro ca
buy tadalafil 20mg for sale buy cialis pill cialis mail order usa
free spins no deposit casino online real money online casinos for real money
best online casinos that payout gambling casino help writing research paper
order ramipril 10mg order glimepiride pills etoricoxib 120mg drug
doxycycline 200mg without prescription buy doxycycline 100mg pills cleocin 150mg oral
order olmesartan online oral calan 240mg depakote 500mg brand
clobetasol medication buy clobetasol online cheap amiodarone order
carvedilol 25mg brand order elavil 10mg sale buy generic elavil
priligy 60mg cheap avanafil order online order motilium 10mg
order indomethacin 75mg generic indomethacin over the counter cheap cenforce
nortriptyline order online panadol brand generic paroxetine 20mg
brand tadacip buy clopidogrel 150mg pills brand amoxicillin
requip 2mg price ropinirole 1mg brand trandate online buy
purchase tricor generic cheap viagra cheap viagra sale
esomeprazole pill buy furosemide without prescription buy furosemide 100mg online cheap
minocin over the counter minocin 100mg price generic terazosin 5mg
order tadalafil 40mg online cheap Get cialis online viagra online
oral glycomet 1000mg buy glucophage 500mg pills buy tamoxifen
order generic modafinil 200mg purchase phenergan generic buy promethazine 25mg online cheap
prednisone 40mg generic deltasone tablet amoxicillin 250mg pill
cost isotretinoin 40mg buy ampicillin 500mg generic purchase ampicillin generic
buy sildenafil 100mg without prescription over the counter ed pills propecia 1mg generic
purchase albuterol generic order augmentin without prescription augmentin 375mg canada
azathioprine 25mg tablet purchase micardis pill naproxen 500mg brand
omnicef 300mg sale order pantoprazole order protonix 20mg
zocor pills phenergan for sale online sildalis ca
dapsone 100 mg ca avlosulfon uk buy atenolol 100mg pills
buy alfuzosin generic order desyrel pill buy generic diltiazem
order phenergan generic cialis delivery buy generic tadalafil
buy generic femara 2.5 mg femara 2.5mg over the counter buy sildenafil 100mg online
buy tadalafil 40mg generic sildenafil 20 mg best place to buy ed pills online
tadalafil 5mg cheap cialis online order otc ed pills
isotretinoin tablet buy amoxicillin 250mg for sale azithromycin 250mg for sale
brand doxycycline 200mg antihistamine pills over the counter augmentin 1000mg pill
buy sumatriptan no prescription purchase imitrex online avodart price
buy flomax sale ondansetron 8mg cheap buy spironolactone 100mg online
purchase betnovate without prescription sporanox 100mg sale buy sporanox
order estrace online order estradiol 1mg buy generic prazosin online
vermox 100mg cost buy generic tretinoin cream order tadalafil 20mg pills
indomethacin 75mg oral terbinafine 250mg generic buy suprax
careprost pill buy desyrel tablets trazodone 50mg oral
generic trimox purchase trimox sale buy clarithromycin 500mg sale
suhagra 50mg pill sildenafil india how to get sildalis without a prescription
order catapres 0.1 mg generic catapres 0.1 mg tablet order spiriva
buy generic minocycline 50mg buy cheap generic minocycline buy pioglitazone 15mg
order isotretinoin 40mg generic order isotretinoin for sale cost azithromycin
buy leflunomide generic leflunomide oral oral azulfidine
buy azipro medication azipro medication gabapentin price
covid ivermectin prednisone 5mg sale deltasone 20mg without prescription
buy lasix no prescription ventolin inhalator price albuterol buy online
levitra uk tizanidine 2mg without prescription oral hydroxychloroquine
buy clobetasol no prescription buy cordarone 200mg sale buy generic amiodarone
benicar cheap order olmesartan pills buy divalproex 250mg without prescription
buy temovate clobetasol uk buy generic amiodarone 200mg
acetazolamide pill imdur 40mg oral buy imuran online
lanoxin buy online buy lanoxin no prescription molnunat medication
oral naprosyn order lansoprazole without prescription buy lansoprazole 15mg generic