TheKrogrammer
TheKrogrammer

Reputation: 92

How to check if an object is null if its 0

$view = new UserView();
var_dump(is_null($view))
echo '<br>';
var_dump($view);

bool(false)

object(UserView)#2 (0) { }

How is this even possible? And how I check if the object is not null?

Upvotes: 0

Views: 76

Answers (2)

Bernardo Silva
Bernardo Silva

Reputation: 22

Try the function get_object_vars https://secure.php.net/manual/en/function.get-object-vars.php

array get_object_vars ( object $object ) Gets the accessible non-static properties of the given object according to scope.

$view = new stdclass();
echo count(get_object_vars($view));

Upvotes: 0

Vincent Decaux
Vincent Decaux

Reputation: 10714

Your object is not null, it's just empty, try :

 $empty = (bool) count((array) $view);

Upvotes: 1

Related Questions