php - __call and __callStatic not working properly or writing incorrectly -
<?php class statics { private static $keyword; public static function __callstatic($name,$args){ self::$keyword = "google"; } public static function tellme(){ echo self::$keyword; } } statics::tellme();
this simple breakdown tried using __construct
way write code statics::tellme();
need write new
__construct
work. , private static variable keyword
not written without being called ideas why not working??
private static $pathname; public function __construct($dir = "") { set_include_path(dirname($_server["document_root"])); if($dir !== "") { $dir = "/".$dir; } self::$pathname = $dir.".htaccess"; if( file_exists(self::$pathname) ) { self::$htaccess = file_get_contents($dir.".htaccess",true); self::$htaccess_array = explode("\n",self::$htaccess); } }
the self::$patname
not getting assigned because not doing $key = new key();
need way if key::get()
or that.
you have misunderstanding in way __callstatic
working. magic method __callstatic
act fallback method when static method unknow class.
class statics { private static $keyword; public static function __callstatic($name,$args){ return 'i '.$name.' , called arguments : '.implode(','$args); } public static function tellme(){ return 'i tellme'; } } echo statics::tellme(); // print tellme echo statics::tellthem(); // print tellthem , called arguments : echo statics::telleveryone('i','love','them'); // print telleveryone , called arguments : i, love, them
so in case :
class statics { private static $keyword; public static function __callstatic($name,$args){ self::$keyword = "google"; return self::$keyword; } } echo statics::tellme();
as per edit :
class statics{ private static $pathname; private static $dir; public function getpathname($dir = "") // or public function getpathname($dir = null) { if($dir !== self::$dir || self::$pathname === ''){ // or if($dir !== null || self::$pathname === ''){ -> way if getpathname() second time, don't have pass param $dir again self::$dir = $dir; set_include_path(dirname($_server["document_root"])); if($dir !== "") { $dir = "/".$dir; } self::$pathname = $dir.".htaccess"; if( file_exists(self::$pathname) ) { self::$htaccess = file_get_contents($dir.".htaccess",true); self::$htaccess_array = explode("\n",self::$htaccess); } } return self::$pathname; } } echo statics::getpathname('some');