Adam Kiss Posted June 28, 2011 Share Posted June 28, 2011 description I know there is this way '!$page->id' to check whether there is anything returned via query, but I really think we could add this 'exists' key. It would be typed boolean, so we wouldn't have to check against type, it would be easily readable. Values false – in NullPage object true – in Page object Usage <?php $page = $pages->get($id); if (!$page->exists) return $error; <?php $page = $this->pages->get($checkID); if ($page->exists){ .. } Link to comment Share on other sites More sharing options...
ryan Posted June 28, 2011 Share Posted June 28, 2011 I like the readability of the $page->exists -- something to consider. But I want to make sure I understand what you are saying, because technically there wouldn't be any difference in how you use it from $page->id, unless I'm not reading it right? i.e. these two would do exactly the same thing: $page = $this->pages->get($id); if($page->id) { // success } $page = $this->pages->get($id); if($page->exists) { // success } I guess what I'm not clear about what would be the benefit of a typed boolean here? Btw, here's another trick to do the same thing. Surround the $page in "quotes" to see if it exists: $page = $this->pages->get($id); if("$page") { // success } 1 Link to comment Share on other sites More sharing options...
Adam Kiss Posted June 28, 2011 Author Share Posted June 28, 2011 Under 'typed boolean' I meant that you may be sure, that the resulting value (whether parent object is Page or NullPage) is boolean, this way 'typed equality' purists (!== vs !=) will have no problem. (!$bool is the same as $bool != true, so in case of '$page->id'. you compare int to bool. With boolean return value, it doesn't matter) Link to comment Share on other sites More sharing options...
ryan Posted June 29, 2011 Share Posted June 29, 2011 Typed equality purists probably aren't using a lot of PHP. But if it makes you happy I'm willing to add it. So I went in to add it, but now it's opening more questions. What would you expect this to return: <?php $page = new Page(); if($page->exists) { // not a NullPage, but not in the DB either } I'm thinking I would expect the above to return false, even though Page is not a NullPage. Thinking on practical terms, a page doesn't 'exist' until it has an ID in the system... But this is different from a NullPage, which is ProcessWire's way of telling you something wasn't found (while allowing you to write code that doesn't have to always check). Basically, it's a question of whether it does this: public function exists() { return !($this instanceof NullPage); } or this: public function exists() { return (bool) $page->id; } I am thinking the second example is the right way, but letting it marinate. Interested in your thoughts. Link to comment Share on other sites More sharing options...
Adam Kiss Posted June 29, 2011 Author Share Posted June 29, 2011 Since I was thinking only in terms of returned queries, you got me by surprise but I'm still inclined to return 'false' if page is not saved yet Because new object of Page, isn't page yet. It's just few fields, I'm inclined to think it exists the moment it has it's position in the page tree, which new Page object doesn't have. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now