note 91104 added to language.oop5.magic
| From: | zach at bygeekz dot com | Date: | Mon, 25 May 2009 19:45:14 +0000 |
| Subject: | note 91104 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-155963@lists.php.net to get a copy of this message | ||
Try this one on.
<?php
$ret = new Test(true);
var_dump((bool)(string)$ret);
var_dump($ret);
$ret=null;
$ret = new Test();
var_dump((bool)(string)$ret);
var_dump($ret);
class Test {
protected $state=null;
function __construct($state=null) {
$this->state = $state;
}
function __toString() {
if ($this->state) { return "1"; } else { return "0"; }
}
}
?>
You could for instance do..
if(!(bool)(string)$ret) { do_something!; }
Alternatively, just make state public, and check it.
if(!$ret->state) {}.
There is no automatic way I have found aside from some internal state check to verify a class. It
will always return an object. The only way around that is to force it out to string either where I
did, or $ret = (string) new Test(); then test the bool of your output..
if (!$ret) { echo "noooo!"; }
But now you have no calling methods, so I hope you passed some data in to get a usable string out.
Of course, if your class isn't named test, you can add a method..
public function test() {
return $this->state;
}
Logically that will work regardless of the _toString(), but I had hoped to post this to help others
see that there are a multitude of ways to check the validity of a class once it is loaded. In
__construct you can add any number of checks and set your state appropriately.
----
Server IP: 208.69.120.35
Probable Submitter: 70.88.182.158
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- https://master.php.net/note/edit/91104
Del: integrated -- https://master.php.net/note/delete/91104/integrated
Del: useless -- https://master.php.net/note/delete/91104/useless
Del: bad code -- https://master.php.net/note/delete/91104/bad+code
Del: spam -- https://master.php.net/note/delete/91104/spam
Del: non-english -- https://master.php.net/note/delete/91104/non-english
Del: in docs -- https://master.php.net/note/delete/91104/in+docs
Del: other reasons-- https://master.php.net/note/delete/91104
Reject -- https://master.php.net/note/reject/91104
Search -- https://master.php.net/manage/user-notes.php