Jump to content

return unset method


osherdo
Go to solution Solved by osherdo,

Recommended Posts

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

 

Link to comment
Share on other sites

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 by QuickOldCar
Link to comment
Share on other sites

  • Solution

@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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.