Page 1 of 1

Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 2:46 am
by networkguy
Hi everyone, new to the place!

I sort of never ask for help until I desperately need it or the time calls for it, so I have come here to ask for help since the time calls for it!
(btw, I'm completely new to oop in php so please take it easy on me)

What I'm posting here is just the utmost basic and essentials to demonstrate what I'm doing and to try and figure out the problem, I'm not interacting with any database or anything like that so it may not make sense to some (at this point in time).

Code: Select all

 
 
<?php
class log {
 
var $author;
var $post;
function __construct() {} //void
 
 
function setAuthor($author) {
    $this->author = $author;
    }
 
function getAuthor() {
    return $this->author;
    }
 
function setPost($post) {
    $this->post = $post;
    }
    
function getPost() {
    return $this->post;
    }
 
}
 
 
class itLog extends log {
 
static $id = 0;
var $itPost;
var $logs;
 
function __construct(&$logs,$author, $post=null){
    parent::__construct();
    $this->logs = &$logs;
    $this->author = $author;
    $this->post = $post;
    self::$id +=1;
    }
    
function setItPost() {
    $this->logs->itPost.= $this->author . $this->post . $this->id;
    }
    
function getItPost() {
   [b] return $this->logs->itPost; [/b]
    }
    
function save() {
    return self::$id; 
    
    }
    
}
 
class opLog extends log {
    static $id = 0;
    var $opPost;
    var $logs;
    
function __construct(&$logs, $author, $post=null) {
    parent::__construct();
    $this->logs = &$logs;
    $this->author = $author;
    $this->post = $post;
    self::$id += 1;
    }
 
function setOpPost() {
    $this->logs->opPost =$this->author . $this->post . $this->id;
    }
    
function getOpPost() {
   [b] return $this->logs->opPost;[/b]
    }
    
function save() {
    return self::$id;
    }
 
}
 
class logs {
    
    var $logs = array();
    var $type;
    var $stringObj; 
    
    function __construct($type) {
    $this->type = $type;
    }   
        
    function setString($stringObj) {
        $this->stringObj = $stringObj;
    }
 
    function getString() {
        return $this->stringObj;
        }
 
    function setLogs($author, $post) {
            $this->stringObj = $this->makeLog($author, $post);
            $this->setString($this->stringObj);
    
            array_push($this->logs, $this->getString());    
    }
        
    function removeLog() 
        {} //not defined
    
    function makeLog($author, $post) {
        if($this->type == 1) {
            return new opLog($this, $author, $post);
            }
        
        if($this->type == -1) {
            return new itLog($this, $author, $post); 
            }
    }
    
    function getLogs() {
        return $this->logs;//return string array            
    }
    
    function display() {
 
        print_r($this->logs);
                    
//iterate through string array 
   [b]     foreach($this->getLogs() as $log=>$obj) {
            echo $log . $obj; }[/b]
            
    }
    
}
 
 
$OpLogs = &new logs(1);
$logOne = $OpLogs->setLogs("Bob", "Hello World!");
$logTwo = $OpLogs->setLogs("Mike", "Hello World!");
$logThree = $OpLogs->setLogs("Moe", "Hello World!");
 
$OpLogs->display();
 ?>
 
 
 
I'm using composition to return a log string object and then add it to the logs array property in the logs class.

The error I am getting is:

Catchable fatal error: Object of class opLog could not be converted to string in /opt/lampp/htdocs/logs.php on line 163

Any help would be greatly appreciated. Using "print_r" function it displays values correctly but not using foreach in display method of the logs class.

Thanks in advance! (and yes, I know I might be doing things not accordingly to specific pattern but this is my first oop php script)
Also, I have searched google and there is nothing I could narrow down as similar problem to mine, only recommendation use "toString()" to convert object to string, I have tried this with no success.

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 3:28 am
by Christopher
My guess would be that the problem is here:

Code: Select all

//iterate through string array 
        foreach($this->getLogs() as $log=>$obj) {
            echo $log . $obj; }[/b]
            
    }

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 3:43 am
by networkguy
arborint wrote:My guess would be that the problem is here:

Code: Select all

//iterate through string array 
&nbsp; &nbsp; &nbsp; &nbsp; foreach($this->getLogs() as $log=>$obj) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $log . $obj; }[/b]
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; }
You mean I'm using foreach improperly? Any ideas how I might change it? I have tried changing this before but same error comes up. Thanks again in advance for any help, if this is true then this caused me alot of pain, I even started smoking again after 3 months successfully quiting because of this error 8O

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 4:00 am
by networkguy
omg, yes you are right I think thats where the error is...

:banghead:

If I echo "blah"; it correctly displays it 3 times which means array contains 3 types of something, but now how to output correct string?

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 4:15 am
by Christopher
I assume that $obj is an object, so you need to call one of its methods that returns a string.

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 4:21 am
by networkguy
New error now when I changed foreach statement to :

foreach($this->logs as $i) { //$this->logs is same thing as before except loose array instead of returned array
echo $this->logs[$i];

error:

Warning: Illegal offset type in...

shows this warning 3 times, again indicating length of array has proper set elements.

I have tried to "cast" the echo output as (string), no luck either.

Any ideas?

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 4:25 am
by networkguy
arborint wrote:I assume that $obj is an object, so you need to call one of its methods that returns a string.

I'm sorry, I'm coming from background in c, this is like the few times I am using a foreach statement. I have changed it multiple times, probably from good to bad to worse (as you saw).

Re: Object of class opLog could not converted to string

Posted: Tue Mar 31, 2009 12:12 pm
by Christopher
The code is a little convoluted, so I can't exactly trace through to know what kind of object $obj is, but my guess would be something like:

Code: Select all

//iterate through string array 
        foreach($this->getLogs() as $log=>$obj) {
            echo $log . $obj->opPost;
        }     
    }