Help In Object of claass could not be converted to string

Discussion in 'PHP' started by Talat50, Nov 10, 2010.

  1. #1
    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
     
    Talat50, Nov 10, 2010 IP
  2. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    xpertdev, Nov 10, 2010 IP
  3. jocurileus

    jocurileus Peon

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    jocurileus, Nov 10, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    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:
     
    gapz101, Nov 10, 2010 IP