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

  • SEARCH
  • Home
  • 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 7044967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:28:21+00:00 2026-05-28T02:28:21+00:00

following piece of code: class a{ public function __get($key) { if($key == ‘obj’) {

  • 0

following piece of code:

 class a{

        public function __get($key) {
            if($key == 'obj') {
                if($b->obj == null) { //PSEUDO this is what I intend to do :)
                   $obj = new Obj();                        
                   $b->obj = $obj
                }
                return $obj;
            }
        }
    }

 class b extends a{

        private $obj = null;


        public function __get($key) {
            return parent::__get($key);
        }

    }

So the idea is to create objects on demand. But I don’t know if it’s possible to detect the class of the object that is calling the parent::_get method. Some operator like child is what I am looking for I guess :).

What is possible but redundant is that for example I have one object called Country, so I define in my class User which has a Country object and another class Location with also has a Country object. Both classes extend class a. I could solve the problem by writing the if block from class a into each of the child classes. But that is what I do not want to do. So it would be easier to be able to check in class a which class called the __get function and to set the desired Country object directly into the child class. I hope my problem becomes clear and it’s not too weird hehe. Though I am open for any other solutions… Thanks.

  • 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-28T02:28:22+00:00Added an answer on May 28, 2026 at 2:28 am

    http://codepad.org/Qnc938Wv

    class a{
    
        public function __get($key) {
            if($key == 'obj') {
                if($this->_obj == null) { //PSEUDO this is what I intend to do :)
                   echo "creating ";
                   $obj = new StdClass();                        
                   $obj->what = get_class($this);
                   $this->_obj = $obj;
                }
                return $this->_obj;
            }
        }
    }
    
    class location extends a{
        protected $_obj = null;
        public function __get($key) {
            return parent::__get($key);
        }
    }
    
    class user extends a{
        protected $_obj = null;
        public function __get($key) {
            return parent::__get($key);
        }
    }
    
    $l = new location();
    echo $l->obj->what . "\n";
    
    $u = new user();
    echo $u->obj->what . "\n";
    
    echo $l->obj->what . "\n";
    echo $u->obj->what . "\n";
    

    This results in

    creating location
    creating user
    location
    user
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code: class Foo { public Foo() { Bar
I have the following piece of code in my DataModel.cs class: public User ValidateUser(string
Let's say we have the following piece of code: public class Event { }
In the following piece of code (C# 2.0): public abstract class ObjectMapperBase< T >
I have the following piece of code: class ICookable { public: virtual void CookMe
I have the following piece of code: class Student { public: Student(){} void display()
I have a piece of code like the following: public class ActivityHelper { public
i was following an ACL tut. which has used this piece of code. class
I have the following piece of code: public class DumpLocationLog extends Thread { LocationManager
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)

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.