php - Calling a non-static method from a static method -
i think basic functionality, please help. how can call non-static method static-method in php.
class country { public function getcountries() { return 'countries'; } public static function countriesdropdown() { $this->getcountries(); } }
preferred way..
it better make getcountries()
method static instead.
<?php class country { public static function getcountries() { return 'countries'; } public static function countriesdropdown() { return self::getcountries(); } } $c = new country(); echo $c::countriesdropdown(); //"prints" countries
adding self
keyword displays php strict standards notice avoid that can create object instance of same class , call method associated it.
calling non-static method static method
<?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
Comments
Post a Comment