Edit report at https://bugs.php.net/bug.php?id=26789&edit=1

 ID:                 26789
 Comment by:         ni...@php.net
 Reported by:        davidc at blesys dot com
 Summary:            NullPointerException desired
 Status:             Wont fix
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   (doesn't apply)
 PHP Version:        5.0.0b3 (beta3)
 Block user comment: N
 Private report:     N

 New Comment:

@3leven11: This will definitely not be a NullPointerException (because PHP 
doesn't have pointers and because PHP does not throw exceptions from core), but 
it could be changed to an E_RECOVERABLE error. You wouldn't be able to 
conveniently catch it, but you could still handle it (e.g. display an error 
page rather than just a WSOD). It think @ircmaxell wanted to make it a 
RECOVERABLE, but I don't know what the state on that currently is.


Previous Comments:
------------------------------------------------------------------------
[2012-10-20 10:21:47] 3leven11 at gmail dot com

@nikic, I completely understand what you are saying, and I have indeed 
altered my code to pass the exception up the chain to the callers of the sub 
class 
constructors. I still think a NullPointerException over a Fatal Error would be 
nice, but that is just my Java developer way of thinking.

I appreciate you taking the time to respond.

------------------------------------------------------------------------
[2012-10-20 09:52:08] paj...@php.net

In addition to Niki's comment, there is no such thing like pointer in PHP.

------------------------------------------------------------------------
[2012-10-20 08:37:27] ni...@php.net

@3leven11: I have a hard time understanding your particular situation. I mean, 
if you can't connect to the database, then you should throw an exception *at 
that point* (PDO actually does it for you, but you choose to silence the 
exception).

If you need this to check for such error conditions then I would highly 
recommend you to just throw an exception as the error occurs rather than 
passing NULLs around everywhere. That's basically the "error code" way of 
handling errors (in your case even without the code) and it indeed involves 
checking every single call. That's why people mostly prefer throwing exceptions.

------------------------------------------------------------------------
[2012-10-20 04:12:14] 3leven11 at gmail dot com

I find the comment by der...@php.net to be very unhelpful.

Testing an object for null every time you use it while being a solution, is a 
very primitive way of ensuring your application does not crash in an ungraceful 
manner. Exception handling, is a much more efficient way of ensuring your 
application can continue running when abnormal events occur.

While I agree that it is up to the developer to handle if an object is null, I 
do see a case where checking for null is an unnecessary performance hit on any 
application. And could be avioded by the implementation of a 
NullPointerException in PHP. Handling errors in code is up to the developers, 
but they can only handle them the best way they know how, with the tools 
provided to them. 

Take the example of an object that you rarely expect to be null, like a PDO 
object. I expect my database to be available 99.9% of the time, are you really 
implying that it is better practice to test for null every I use this object 
rather than enclose my use of a PDO object within a try catch block, handling 
any exception that may arise?

example code:

// base class

namespace lib\translator;
use lib\database as db;

class EntityTranslator {
    protected $dbConn;
    
    public function __construct() {
        try {
            $this->dbConn = db\Database::getInstance();
        }
        catch(\PDOException $e) {
            // do some error logging here
        }
    }
}


// subclass

namespace lib\translator;
use lib\entity as entity;

class OrderRequestTranslator extends EntityTranslator {
    
    public function __construct() {
        parent::__construct();
    }
    
    public function createOrderRequest() {
        $request = new entity\Request();
        try {
            $stmt = $this->dbConn->prepare("CALL createRequest()");
            $stmt->execute();

            $rowCount = $stmt->rowCount();
            if ($rowCount == 1) {
                $row = $stmt->fetch(\PDO::FETCH_ASSOC);
                
                // do stuff with the data here
            }
            return $request;
        }
        catch (\PDOException $pdoe) {
            // do error logging here
        }
        catch (\Exception $ex) {
            // do error logging here
        }
    }
}


So in the above example, if there is an exception while connecting to the 
database, which as I pointed out I expect to very rare, and it may actually 
never happen, that before issuing the call to $this->dbConn I should check if 
$this->dbConn is null?

This is the perfect case for where my catch(\Exception $ex) {} block would be 
perfect. I really never expect to be in that code, from my point of view, and 
correct me if I am wrong, I have not coded my application in an incorrect way, 
except for the fact that PHP has no NullPointerException.

------------------------------------------------------------------------
[2012-03-13 22:22:56] me at joshsera dot com

The point of an NPE is so you can enclose code in a try/catch block instead of 
wrapping every statement in an if that tests for null. It makes for more 
readable 
code.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=26789


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=26789&edit=1

Reply via email to