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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:46:13+00:00 2026-06-09T07:46:13+00:00

I’m trying to setup some classes to work with, but I’m not really sure

  • 0

I’m trying to setup some classes to work with, but I’m not really sure how to accomplish to following.

Let’s say I have a class “foo” that I’m working in, and I want to use functions from another class called “loader”. The purpose of this loader class is to load various other classes that I can then use within the “foo” class.

Example:

class foo {
    function foo() {
        $this->load->model();
    }
}

For this to work I would first initiate the class “load”:

class foo {
    function foo() {
        $this->load = new loader;
        $this->load->model('some_model');
    }
}

Now I can use the functions from within the loader class. The next thing I want is for the loaded model to be accessible from within the class foo. Example:

class foo {
    function foo() {
        $this->load = new loader;
        $this->load->model('some_model');
        $this->some_model->function_from_this_model();
    }
}

And this is where I get lost, because I’m not sure how to stay within the scope of the “foo” class. At any given time I’d like to be able to use $this->load->model('some_model') to load a new model, that becomes accessible through $this->some_model. Or $this->load->something_else('some_name') which becomes available through $this->some_name.

Here’s a loader class example:

class loader {
    function model($model_name) {
        require('models/'.$model_name.'.php');
        $model = new $model_name;
        // and what to do now, to get it back to the $this var from class foo?
    }
}

But all of this happens only within the scope of the loader class.

Any ideas on how to accomplish this?

Edit:
I know I can assign it directly to a variable, like $this->model_name = $this->load->model('model_name'), but that’s what I’m trying to avoid. I’d like to be able to use this “load” subclass to be able to work within the scope of the class “foo”.

  • 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-09T07:46:15+00:00Added an answer on June 9, 2026 at 7:46 am

    You can use a Singleton, which will allow you to initialize your class objects from another class.

    Example:

    // Loader will access class singleton and set object
    class loader {
        function model($model_name, $class) {
            require('models/'.$model_name.'.php');
            $class::init()->$model_name = new $model_name;
        }
    }
    
    // see new singleton methods
    class foo {
    
        private static $instance
        public static function init(){
            if(is_null(self::$instance)){
                $class = __CLASS__;
                self::$instance = new $class;
            }
    
            return self::$instance;
        }
    
        function foo() {
            $this->load = new loader;
            $this->some_model = $this->load->model('some_model', __CLASS__);
            $this->some_model->function_from_this_model();
        }
    }
    
    // init class like this
    foo::init()->foo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.