osherdo Posted January 28, 2016 Share Posted January 28, 2016 I am trying to return the unset method (with the __destruct function) but it only returns the error at the top of th page and not at the bottom as it should be. Can you tell me how do I return it in the bottom please? here's my code: <?php echo "Fourth drill:"."<br><br>"; class MyClass4 { public $prop1 = "I'm a class property!"; public function __construct() { echo 'The class "', __CLASS__, '" was initiated!<br />'; } public function __destruct() { echo 'The class "', __CLASS__, '" was destroyed.<br />'; } public function __toString() { echo "Using the toString method: "; return $this->getProperty(); } public function setProperty($newval) { $this->prop1 = $newval; } public function getProperty() { return $this->prop1 . "<br />"; } } class MyOtherClass extends MyClass4 { public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; } } // Create a new object $newobj = new MyOtherClass; // Output the object as a string echo $newobj->newMethod(); // Use a method from the parent class. echo $newobj->getProperty(); ?> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 28, 2016 Share Posted January 28, 2016 (edited) If you echo within it will echo wherever it was included, instead return the result and echo where using it. As for where it is displayed on a page, that is the job for css style, shouldn't use style in function results. Edited January 28, 2016 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
requinix Posted January 29, 2016 Share Posted January 29, 2016 It is executing in the proper order: https://3v4l.org/7lALJ. Quote Link to comment Share on other sites More sharing options...
Solution osherdo Posted January 29, 2016 Author Solution Share Posted January 29, 2016 @QuickOldCar I am not sure I got what your'e trying to say. anyway here's a screenshot of how it did look when posting this problem here: http://4.1m.yt/8qLiQI9.png and after I changed the code to look like this - calling the unset method explicitly it now shows it properly: (I post the second class only for convenience): class MyOtherClass extends MyClass { public function __construct() { echo "A new constructor in " . __CLASS__ . ".<br />"; } public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; } } // Create a new object $newobj = new MyOtherClass; // Output the object as a string echo $newobj->newMethod(); // Use a method from the parent class echo $newobj->getProperty(); unset($newobj); generate messsage on browser. ?> And now it shows well: http://i63.tinypic.com/x4g4tc.png Quote Link to comment 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.