/    Sign up×
Community /Pin to ProfileBookmark

Object ISSET

So I want to check if my object has been created.

I’d have thought

`if (isset($myObject)){ }`

Would have done the trick but you can’t. What’s the ‘best practice’ way of doing something like this?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorFeb 01.2022 — Is using is_null accetable?
Copy linkTweet thisAlerts:
@NogDogFeb 01.2022 — Assuming you want to make sure it's a particular type of object, maybe...
[code=php]
if(isset($myObject) and $myObject instanceof SomeClassNameHere) { }
[/code]

If all you care is that it's _any_ type of object, you could replace the instanceof expression with an [u][is_object()](https://www.php.net/manual/en/function.is-object.php)[/u] call instead, I suppose.
Copy linkTweet thisAlerts:
@NogDogFeb 01.2022 — PS: If you just want to make sure an object creation worked:
[code=php]
$foo = new Something();
if(empty($foo)) {
throw new Exception("Oops");
}
[/code]

(Though there's a good chance it will have already thrown an Exception, I suspect?)
Copy linkTweet thisAlerts:
@kiwisauthorFeb 01.2022 — Sorry, I've just realized. When I create the object. I simply pass in a database so it'll always be created.

I populate all the properties by calling a method loadFromID

I pass in a ID which relates to a a database ID.

I need to check if something has been returned and properties populated or not.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...