Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7584443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:55:18+00:00 2026-05-30T18:55:18+00:00

I recently had a short discussion with another developer about the relationship between PHP’s

  • 0

I recently had a short discussion with another developer about the relationship between PHP’s __isset() and __get() magic methods. It was brought about by a class that we have which lazy-loads other objects via the __get() method (lazy-loading meaning that the property doesn’t exist until first accessed, at which point the object is instantiated and returned). We had differing opinions on what __isset() should return for a property that has yet to be loaded, though. The property technically doesn’t exist (it isn’t set, or at the very least, it’s set but currently NULL), but a call to it will also technically succeed (barring any exceptions) and return a non-NULL value.

So, my question is, in this situation should __isset() simply be an indicator of whether or not __get() will succeed for the same argument (return TRUE if __get() will succeed and return a non-NULL value). Or, should it behave more technically, and return FALSE, since the data does not yet exist (even though it will when first accessed)?

A simple example:

class Foo {
    protected $data;

    public function __get($prop) {
        if ($prop == 'bar') {
            $this->data['bar'] = new Bar;
            return $this->data['bar'];
        }
    }

    public function __isset($prop) {
        if ($prop == 'bar') {
            // What goes here?
            // return isset($this->data[$prop]) would mean
            // that the first call to isset($foo->bar) below will be FALSE
            // which means that using logic like this would always fail
            // and __get() would never be called:
            // isset($foo->bar) ? $foo->bar->baz : 'foo->bar not set'
        }
    }
}

class Bar {}

$foo = new Foo;
var_dump(isset($foo->bar)); // ???
$bar = $foo->bar;
var_dump(isset($foo->bar)); // bool(true)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:55:19+00:00Added an answer on May 30, 2026 at 6:55 pm

    You have to consider this from the perspective of a user of the class. Assume that $foo is of class Foo, and you have this code:

    if(isset($foo->bar)) {
        var_dump($foo->bar); // #1
    }
    else {
        // $foo->bar is "not set", right?
        $x = $foo->bar;
        var_dump($x); // #2
    }
    

    Rhetorical questions: would you expect #1 to ever print null? Would you expect #2 to print anything except null?

    Of course not. If it worked like that, then users of class Foo would spend most of their working day cursing the author.

    Assuming that I have convinced you, it’s quite simple to start from the desired behavior and work back to how it should be achieved, namely to implement __isset like this:

    public function __isset($prop) {
        $val = $this->$prop;
        return isset($val);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had a discussion on another forum with another developer and the topic
I recently had to work on a project where the previous developer modified the
I recently had a need to interpret a DEC 32-bit floating point representation. It
We recently had a code review . One of my classes was used so
I recently had a problem during the deployment of a windows service. Four computers
I recently had a problem in my app where some of the subviews I
I recently had to take a quick look at Adobe InDesign server. In this
We recently had a new requirement to use the phonon component of Qt, which
I recently had some problems with my VS 2008, and was recommended to reinstall.
I recently had cause to work with some Visual Studio C++ projects with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.