technoViking Posted April 23, 2009 Share Posted April 23, 2009 Hi guys, I'm not new to programming (C/C++/Java background) but I am new to php, just started a few days ago and I'm confused on how exactly I would go about printing out the information inside an object. For example my class is this: <?php class Task { public $iid; public $name; public $amountCompleted; public $priority; public $dueDate; public $estimatedNumberOfHours; public $parentId; public $prereqId; public function __construct($iid = -1, $name='', $amountCompleted=0, $priority=-1, $dueDate='2009-01-01 00:00:00', $estimatedNumberOfHours = 0, $parentId=-1, $prereqId=-1) { $this->iid = $iid; $this->name = $name; $this->amountCompleted = $amountCompleted; $this->priority = $priority; $this->dueDate = $dueDate; $this->estimatedNumberOfHours = $estimatedNumberOfHours; $this->parentId =$parentId; $this->parentId =$parentId; } function toString() { return "$iid, $name, $amountCompleted, $priority, $dueDate, $estimatedNumberOfHours, $parentId, $parentId"; } } ?> I'm calling the function like this: //testing the function $test = array(); $test = getResult('SELECT * FROM tasks','TASK'); foreach($test as $value) { print "$value->toString()<br>"; } Here is the full code if you need it. <?php require('databaseFunctions.php'); require('Classes/MeetingClass.php'); require('Classes/TaskClass.php'); require('Classes/ItemClass.php'); //testing the function $test = array(); $test = getResult('SELECT * FROM tasks','TASK'); foreach($test as $value) { print "$value->toString()<br>"; } //getResult($query, $itemType) //@input: SQL query, item Type //@output: an array of ItemType that contains the result set of the query //NOTE: ItemType can be: TASK, or MEETING, please use CAPS when passing function getResult($query, $itemType) { //stores all the objects which were populated by the rows of the query result set $objectArray = array(); //$taskCount = 8; //$meetingCount = 3; $db = getDBConnection(); $result = $db->query($query); if($result) { $i = 0; while($row = $result->fetch_assoc()) { if($itemType == 'TASK') { $tempTask = new Task(); //populating object attributes $tempTask->iid = $row['iid']; $tempTask->name = $row['name']; $tempTask->amountCompleted = $row['amountCompleted']; $tempTask->priority = $row['priority']; $tempTask->dueDate = $row['dueDATE']; $tempTask->estimatedNumberOfHours = $row['estimatedNumberOfHours']; $tempTask->parentId = $row['parentId']; $tempTask->prereqId = $row['prereqId']; //putting object into an array $objectArray[$i++] = $tempTask; } else if($itemType == 'MEETING') { $tempMeeting = new Meeting(); //populating object attributes $tempMeeting->iid = $row['iid']; $tmepMeeting->subject = $row['subject']; $tempMeeting->name = $row['name']; $objectArray[$i++] = $tempMeeting; } else { printf("Input error, needs MEETING or TASK\n"); } } //return the array that will be used for further processing return $objectArray; } } ?> But the errors I"m getting are the following: Notice: Undefined property: Task::$toString in C:\wamp\www\JustToDoIt.com\retrieve.php on line 11 () shouldn't $value be of type Task? So I'm trying to call the toString() function to print out the data inside the object. to make sure its storing the objects correctly in the array. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/155279-how-can-i-print-the-data-inside-an-object/ Share on other sites More sharing options...
Maq Posted April 23, 2009 Share Posted April 23, 2009 I'm not seeing any blatant issues with your code. It seems to me that you're creating and returning the array of objects correctly. One thing I noticed, in your toString() method, you should use $this before all of your variables that are being returned. I assume you have error_reporting() set on max: ini_set ("display_errors", "1"); error_reporting(E_ALL); What version of PHP are you using? Quote Link to comment https://forums.phpfreaks.com/topic/155279-how-can-i-print-the-data-inside-an-object/#findComment-816974 Share on other sites More sharing options...
technoViking Posted April 23, 2009 Author Share Posted April 23, 2009 Thanks for the reply and Yes error code is max...this is the exact error: Notice: Undefined property: Task::$toString in C:\wamp\www\JustToDoIt.com\retrieve.php on line 14 () Notice: Undefined property: Task::$toString in C:\wamp\www\JustToDoIt.com\retrieve.php on line 14 () Notice: Undefined property: Task::$toString in C:\wamp\www\JustToDoIt.com\retrieve.php on line 14 () Notice: Undefined property: Task::$toString in C:\wamp\www\JustToDoIt.com\retrieve.php on line 14 () I'm returning an array of object and i'm wanting to loop through that array of objects and print the data members values...thats why I wrote the toString() function. The good news is, it looks like the array does contain the objects...because that query result would have 4 Task objects, the problem is just printing its information. I'm using: 5-2.9.1 also using WAMPSERVER 2.0 to do all this Quote Link to comment https://forums.phpfreaks.com/topic/155279-how-can-i-print-the-data-inside-an-object/#findComment-817000 Share on other sites More sharing options...
Maq Posted April 23, 2009 Share Posted April 23, 2009 You're probably going to pound your head against the wall, cause I just did. Change this line to: print $value->toString() . " "; BTW: Are you TechnoViking from Ubuntu forums? Oh and sorry for the delayed response, I went to the gym Quote Link to comment https://forums.phpfreaks.com/topic/155279-how-can-i-print-the-data-inside-an-object/#findComment-817027 Share on other sites More sharing options...
technoViking Posted April 23, 2009 Author Share Posted April 23, 2009 Awesome thanks man! I was going nuts! I'm not actually, I use this name on Overclockers forum Quote Link to comment https://forums.phpfreaks.com/topic/155279-how-can-i-print-the-data-inside-an-object/#findComment-817046 Share on other sites More sharing options...
Maq Posted April 23, 2009 Share Posted April 23, 2009 Awesome thanks man! I was going nuts! I'm not actually, I use this name on Overclockers forum Ah, ok, the reason I asked is because technoViking isn't a very common name lol. Glad it works, it's always the little things that throw you off. If this is solved you can click the [sOLVED] tab at the bottom Quote Link to comment https://forums.phpfreaks.com/topic/155279-how-can-i-print-the-data-inside-an-object/#findComment-817049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.