lopes_andre Posted December 17, 2010 Share Posted December 17, 2010 Hi, I'm newbie to php opp. I just need to know if it is possible to instantiate a class like this? $import = new geoIpImportCSV(obtainDownloadFileName(), 'table1'); class geoIpImportCSV { private $input_zip_file; private $db_table; function __construct($input_zip_file, $db_table) { $this->input_zip_file = $input_zip_file; $this->db_table = $db_table; } /* "GeoLiteCity_" . $date . ".zip". */ function obtainDownloadFileName() { return "GeoLiteCity_20101201.zip"; } // ... } It is possible to call a method(obtainDownloadFileName()) like this to the constructor? Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/221994-question-about-instantiate-a-class/ Share on other sites More sharing options...
DavidAM Posted December 17, 2010 Share Posted December 17, 2010 It IS possible to call a function as a parameter to another function call. HOWEVER, the function (as a parameter) has to resolve completely BEFORE the outer function is called. So the object will NOT exist when the (parameter) function is called. [by the way, the way you have it written obtainDownloadFileName() is a normal function call NOT a class or object method call] You could pass the method name as a string and have the constructor call it: class clsTryme { function __construct($funcToGetFile, $table) { $this->fileName = $this->$funcToGetFile(); $this->table = $table; } function getGeoFile() { return 'geo_file.zip'; } } $obj = new clsTryme('getGeoFile', 'table1'); or just have the constructor call the method without passing it in: class clsTryme { function __construct($table) { $this->fileName = $this->getGeoFile(); $this->table = $table; } function getGeoFile() { return 'geo_file.zip'; } } $obj = new clsTryme('table1'); IF your getGeoFile() method is a STATIC method, then you can call it in the call to the constructor: class clsTryme { function __construct($filename, $table) { $this->fileName = $filename; $this->table = $table; } static function getGeoFile() { return 'geo_file.zip'; } } $obj = new clsTryme(clsTryme::getGeoFile(), 'table1'); Quote Link to comment https://forums.phpfreaks.com/topic/221994-question-about-instantiate-a-class/#findComment-1148751 Share on other sites More sharing options...
lopes_andre Posted December 17, 2010 Author Share Posted December 17, 2010 Thanks for your help! Now I'm without doubts. Best Regards, Quote Link to comment https://forums.phpfreaks.com/topic/221994-question-about-instantiate-a-class/#findComment-1148753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.