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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:52:07+00:00 2026-05-27T17:52:07+00:00

Been searching all around but still cannot find a solution for this problem. My

  • 0

Been searching all around but still cannot find a solution for this problem.
My problem is that i got these snips of code(Examples):

Core file

class Core {
    public $DB = null;
    public $Handler = null;

    function run() {
        $this->DB = "somedatabase";

        include_once('handler.php');
        $this->Handler = new Handler;

        $this->Handler->run();
    }
}

This is the helper.php example

class Handler extends Core {
    function run() {
        echo "<pre>"; print_r($this); echo "</pre>"; die();
    }
}

Even tho i defined the DB variable before i include the helper then it is still empty inside the helper class. It’s defined yes but it’s empty. Which means it properly doesn’t share the same memory as the Core class.

Keep in mind that the Core class it self is instanced too.

–

Thanks for all suggestions

Edit

PhpMyCoder got it right. Thank you for the detailed and well written reply.
For over 2 years i been seeing PHP scopes as being the same or sorta the same as JavaScript’s scope. Now i realize that if i extend my “Core” class i get all the methods and properties within it. But the values is private to my class and my class alone.

This is great. Finally i got it.

  • 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-27T17:52:08+00:00Added an answer on May 27, 2026 at 5:52 pm

    From what I gather here you are talking about public instance variables. They are performing as OOP would require. Each time you instantiate a class with

    $core = new Core();  // or
    $handler = new Handler();
    

    Each of them gets a fresh space in memory to store their instance variables. Instance variables are unique to each instance of a class, as the name would suggest. So, two separate instances of Core and Handler do not share instance variables. However since Handler extends Core, two instances of Core are created. One instance is the one that I created on the first line. The other is created so that Handler can extend it on the second line. These two instances of Core are not the same object. To have the same values for Core across all core objects you will need to use static (class) variables.

    class Core {
        public static $hello = 'World';
    }
    
    var_dump(Core::$hello); //string('Word')
    

    In my example, $hello will always be available to everyone by accessing it with the scope resolution operator, ::. So Handler could access it with either Core::$hello or parent::$hello. If you wanted to only expose this static variable to Core and its subclasses, then you would need to make it protected and access it from within Core with self::$hello and from its subclasses with parent::$hello.

    class Core {
        protected static $hello = 'World';
    
        public function sayHello() {
            echo 'Hello '.self::$hello;  //from within Core, access with `self`
        }
    }
    
    class Handler extends Core {
        public function myParentSays() {
            echo 'My parent says: Hello '.parent::$hello;
        }
    }
    
    $core = new Core();
    $core->sayHello(); // 'Hello World'
    
    $handler = new Handler();
    $handler->myParentSays(); // 'My parent says: Hello World'
    

    Check the PHP docs for more on the static keyword and the scope resolution operator.


    EDIT
    I believe your confusion lies in a misunderstanding of how inheritance works in OOP so let me give you a little real-world-ish example. Let’s say you create a class for employees called Employee. This class has a public instance variable (that is, one that can be accessed with ->) for the name of the person. In PHP this would be:

    class Employee {
        public $name;
    
        public __construct($name) {
            $this->name = $name;
        }
    }     
    

    Now let’s create a new employee:

    $tim = new Employee('Tim');
    

    Let’s say that we need a new class, Intern, that should subclass Employee. That should be easy enough:

    class Intern extends Employee {
        public function makeCoffee(Employee $receiver) {}
    }
    

    If we create a new intern now, should his name be Time just because we have already created another employee named Tim? No. That doesn’t make sense.

    $intern = new Intern();
    var_dump($intern->name); //string(0) ""
    

    Now say that setting the name was some complicated and arduous process and we’d rather not have to code it again. With a little modification to our Intern class we can leave the name setting to its superclass, Employee.

    class Intern {
        public function __construct($name) {
            parent::__construct($name);
        }
    
        public function makeCoffee(Employee $receiver) {}
    }
    

    Now we can create a new intern and set his or her name. Notice how the other Employee keeps his name.

    $intern = new Intern('Something Forgettable');
    var_dump($intern->name); // string(21) "Something Forgettable"
    var_dump($employee->name); // string(3) "Tim"
    

    Now why is this? In OOP, a subclass/superclass is an “is a” relationship. The Intern “is an” Employee. The Intern has all the same properties and methods as an Employee but because each Intern and Employee are distinct they have their own values for these properties.

    With this in mind, I suggest you rethink your strategy for your classes. Does it really make sense that Handler is a Core? Does it make sense that MainController is a Handler?

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

Sidebar

Related Questions

I've been searching around but I just can't seem to find what I'm looking
I think this question is probably fairly simple, but I've been searching around and
I have been searching around for answers, but I can't seem to find anything.
I've been searching around, and I haven't found how I would do this from
Yeah so I've been messing around with javascript a while but only recently got
I have been searching around the web all day for the best way to
I've been doing alot of searching around the website but not even sure if
I have been searching all of the net but I can not seem to
I've been searching around for a jquery plugin or some js source code that
I have been searching around for a SQL function that tests a value for

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.