我认为这是非常基本的功能,请帮忙。如何在php中将非静态方法调用为静态方法。
class Country {
public function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
$this->getCountries();
}
}
最好将getCountries()
方法改为静态。
<?php
class Country {
public static function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
return self::getCountries();
}
}
$c = new Country();
echo $c::countriesDropdown(); //"prints" countries
添加self
关键字将显示PHP Strict Standards注意为了避免您可以创建相同类的对象实例并调用与之关联的方法。
<?php
class Country {
public function getCountries() {
return 'countries';
}
public static function countriesDropdown() {
$c = new Country();
return $c->getCountries();
}
}
$c = new Country();
echo $c::countriesDropdown(); //"prints" countries
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句