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 8955925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:40:00+00:00 2026-06-15T14:40:00+00:00

I have a base class that I need to call functions on a class

  • 0

I have a base class that I need to call functions on a class that is referenced in the child class.

Easy enough,

class base_class {

    public function doSomethingWithReference(){
        $this->reference->doSomething();
    }
}

class extended_class extends base_class{

    protected $reference;

    public function __construct($ref){
        $this->reference = $ref;
    }
}

Now this works fine obviously,

But, when I am debugging I don’t care about the value of $this->reference

But, the object that $this->reference refers to is huge!

so, when I do print_r($instanceOfExtendedClass) I get the print out of that object.

Now the reference is different for each class that extends base_class.

What I wanted to do was just set reference as a static property on the extended_class class.

But, then changing doSomethingWithReference to be self::$reference throws an undefined variable error.

And conversely setting the static variable in base_class and modifying it from extended_class doesn’t work as it changes the variable for all everything that extends from that class.

Is there any way to do this so I don’t get the print out of $this->reference?

  • 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-06-15T14:40:01+00:00Added an answer on June 15, 2026 at 2:40 pm

    Because you are using PHP 5.3, you can use late static binding to resolve the static call to the right class at runtime.

    class base_class {
    
        public function doSomethingWithReference(){
            static::$reference->doSomething();
        }
    
    }
    
    class extended_class extends base_class{
    
        protected static $reference;
    
        public function __construct($ref){
            static::$reference = $ref;
        }
    
    }
    

    Big fat reminder: That one extended_class::$reference is going to be shared amongst all of the extended_class instances. If that is not what you intend, this is not going to work.

    You seem to actually be worried about memory or resource use. In PHP, all objects are passed by reference. This means that passing an object as an argument, or creating a copy of it, etc, does not consume extra memory. If you need to reference an object in a number of other objects, doing so will not consume extra memory.


    If I had extended_class and another identical class (say extended_class1) would they share the reference as well? or would all extended_class’ instances share one reference, while all extended_class1′ instances would share another (the ideal case)?

    It looks like the sharing is based on where the static variable is defined. Two examples, both from the PHP interactive prompt:

    php > class Shared { public $me; public function __construct($me) { $this->me = $me; } }
    php > class Base { protected static $ref; public function foo() { echo static::$ref->me, "\n"; } }
    php > class Inherit_1 extends Base { public function __construct($ref) { static::$ref = $ref; } }
    php > class Inherit_2 extends Base { public function __construct($ref) { static::$ref = $ref; } }
    php > class Inherit_3 extends Inherit_1 {}
    php > $shared_1 = new Shared(1)
    php > ;
    php > $shared_2 = new Shared(2);
    php > $shared_3 = new Shared(3);
    php >
    php > $in_1 = new Inherit_1($shared_1);
    php > $in_2 = new Inherit_2($shared_2);
    php > $in_3 = new Inherit_3($shared_3);
    php >
    php > $in_1->foo();
    3
    php > $in_2->foo();
    3
    php > $in_3->foo();
    3
    

    In this case, because the reference lives in the base class, everyone sees the same one. I suppose this makes some sort of sense.

    What happens when we declare the reference with each child class.. most of the time?

    php > class Shared { public $me; public function __construct($me) { $this->me = $me; } }
    php > class Base { public function foo() { echo static::$ref->me, "\n"; } }
    php > class Inherit_1 extends Base { protected static $ref; public function __construct($ref) { static::$ref = $ref; } }
    php > class Inherit_2 extends Base { protected static $ref; public function __construct($ref) { static::$ref = $ref; } }
    php > class Inherit_3 extends Inherit_1 {}
    php > class Inherit_4 extends Inherit_1 { protected static $ref; }
    php > $shared_1 = new Shared(1);
    php > $shared_2 = new Shared(2);
    php > $shared_3 = new Shared(3);
    php > $shared_4 = new Shared(4);
    php > $in_1 = new Inherit_1($shared_1);
    php > $in_2 = new Inherit_2($shared_2);
    php > $in_3 = new Inherit_3($shared_3);
    php > $in_4 = new Inherit_4($shared_4);
    php > $in_1->foo();
    3
    php > $in_2->foo();
    2
    php > $in_3->foo();
    3
    php > $in_4->foo();
    4
    

    Because 3 inherited from 1 without declaring it’s own static property, it inherited 1’s. When we set 3’s to Shared(3), it overwrote 1’s existing Shared(1).

    Conclusion: For this to work, the property needs to be declared in every class that needs the single unique reference. Note that this code is valid as of 5.4.x.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Base class and a Child class that inherits from Base
I have an abstract immutable base class that defines enforces child classes to be
For a policy-based class-design I need some of the policies to call functions that
I have a Base class that needs a Generic type. That can be either
I have a base class ( car ) and a class that inherit the
In the project I'm working on now, we have base Entity class that looks
I have a Generic Base Class that I want to allow one of two
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
I have a class that inherits from a base class (this contains a lot

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.