Hello dears i am having this error Help In Object of claass could not be converted to string can any one guide me? //////////////////////////////////////////////// <?php define('DIR_LANGUAGE', ' C:\wamp\www\mvc\mvc/'); final class Language { private $directory; private $data = array(); //public function __construct($directory) //{ // $this->directory = $directory; //} // public function get($key) { // return (isset($this->data[$key]) ? $this->data[$key] : $key); //} public function load($filename) { $_ = array(); $default = DIR_LANGUAGE . 'english/' . $filename . '.php';//Error on this line 19 if (file_exists($default)) { require($default); } $file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php'; if (file_exists($file) && $file != $default) { require($file); } $this->data = array_merge($this->data, $_); return $this->data; } } ?> /////////////////////////////////////////// <?php $language=new Language();//Making Object Of above class: $language->load($language);//calling Function Load Of above class: ?> ////////////////////////////////////////////////////////////////// Error: Object of class Language could not be converted to string in C:\wamp\www\mvc\mvc\controller\common\language.php on line 19
hi, In the function initialization, parameter considered as string and you are passing the object of the own class. if you require object to use in that function then u can use $this for that. Hope this helps Avi
PHP is complaining about the class being passed and then used as a string through the concatenation you are trying to do for the $default variable. When the conversion happens from object to string that object calls the magic method __toString() (I told you this because you can make it work by declaring the __toString() method and use it this way). But as xpertdev said your current problem is using your object as a parameter for you class methods. That is this part : $language=new Language();//Making Object Of above class: $language->load($language);//calling Function Load Of above class: <-- here is your problem, $language is your instantiation of your Language class one line higher PHP:
load function needs a string parameter that's it. <?php $lang1 = 'english'; $language=new Language();//Making Object Of above class: $language->load($lang1);//calling Function Load Of above class: ?> PHP: