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();
?>
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.