bugcoder Posted January 3, 2012 Share Posted January 3, 2012 i have a class class UploadHandler { public $options; function __construct($options=null) { $this->options = array( 'upload_dir' => ROOT.DS.'files'.DS.'Jupload/', 'param_name' => 'files', 'max_file_size' => null, 'min_file_size' => 1 ); if ($options) { $this->options = array_replace_recursive($this->options, $options); } } .................other code } How can i change the options $this->options['upload_dir'] by calling this class in other class? Please help with example. Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/ Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 // At construction $upload_handler = new UploadHandler(array( 'upload_dir' => '....' )); // After $upload_handler->options['upload_dir'] = '...'; Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303748 Share on other sites More sharing options...
bugcoder Posted January 3, 2012 Author Share Posted January 3, 2012 // At construction $upload_handler = new UploadHandler(array( 'upload_dir' => '....' )); Sorry couldnt get how to use your code when making object from class? upload_dir is index of options array...so wont it be different? Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303763 Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 if ($options) { $this->options = array_replace_recursive($this->options, $options); } This condition checks if you passed any $options to the constructor method, and if so replaces them in $this->options. So although you do originally define the 'upload_dir' key, the call to array_replace_recursive() overwrites it. Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303767 Share on other sites More sharing options...
bugcoder Posted January 3, 2012 Author Share Posted January 3, 2012 but im getting error when iniating class like this $upload_handler = new UploadHandler( array("options" => array("upload_dir"=>ROOT.DS.'files'.DS.'newDire') )); the error is Fatal error: Call to undefined function array_replace_recursive() in D:\xampp\htdocs\... Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303771 Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 That's not what I posted before. The parameter in the constructor accepts an array of options, not an array with an 'options' key containing an array of options. Structure of yours: array( "options" => array( "upload_dir" => ROOT.DS.'files'.DS.'newDire' ) ) Mine: array( "upload_dir" => ROOT.DS.'files'.DS.'newDire' ) Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303772 Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 Also you're getting that error because array_replace_recursive() is only available in PHP v5.3 and later. Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303773 Share on other sites More sharing options...
bugcoder Posted January 3, 2012 Author Share Posted January 3, 2012 yes i did use the same code you said but i got the same error message again... Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303776 Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 Check my latest reply.. Didn't notice your edit when I posted my first. Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303777 Share on other sites More sharing options...
bugcoder Posted January 3, 2012 Author Share Posted January 3, 2012 Thank you a lot for your great help indeed in both OOP and 5.3 version issue....................... yes it was not working also coz of 5.3 version now from php site i have found function that does same work as array_replace_recursive from this link and my code worked... link is http://php.net/manual/en/function.array-replace-recursive.php and its user contributed code by "Gregor at der-meyer dot de" Thanks again Adam!!! Link to comment https://forums.phpfreaks.com/topic/254278-oop-help/#findComment-1303780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.