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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:13:18+00:00 2026-05-29T16:13:18+00:00

I’m trying to load libraries dynamically with a loop then add them to $this

  • 0

I’m trying to load libraries dynamically with a loop then add them to $this and I’m trying to use instanceof to see if it’s already been initiated before trying to initiate it again:

    $libraries = array('strings');
    foreach ($libraries as $lib) {
        require_once('lib/'.$lib.'.lib.php');
        if(!($this->lib_{$lib} instanceof $lib)){
            $this->lib_{$lib} = new $lib();
        } else {
            continue;
        }
    }

But when I try this I just get an 500 internal server error from apache.

The error I get is:

[15-Feb-2012 10:31:21] PHP Notice:  Undefined property: load::$lib_ in /home/wwwsrv/public_html/system/core.lib.php on line 57

(line 57 refers to the line with the if statement)

Any ideas?

  • 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-29T16:13:19+00:00Added an answer on May 29, 2026 at 4:13 pm

    Looks like you don’t have a member variable initialized which matches YOUR_CLASS::$lib_SOMELIB, so PHP is throwing a notice.

    I would try something like:

    if ((!isset($this->lib_{$lib})) || 
        (!($this->lib_{$lib} instanceof $lib))) {
    

    Also, you may want to look into using magic methods; __set and __get to dynamically set member variables of YOUR_CLASS, so you don’t need to maintain a laundry list.

    — Edit #2 —

    Not sure why you are getting the stdObject error, might be that already tried initializing the value earlier. Take a look at this snippet, and see if it helps. I tested it and works.

    class strings
    {
        public function __toString()
        {
            return __CLASS__;
        }
    }
    
    class MyClass
    {
        private $dynamicMembers = array();
    
        public function loadLibs()
        {
            $libraries = array('strings');
    
            foreach ($libraries as $lib) {
    
                // Had to do it this way, b/c it was resulting in lib_ and not lib_strings
                $memberName = 'lib_' . $lib;
    
                if ((!isset($this->$memberName)) ||
                    (!($this->$memberName instanceof $lib))) {
                    var_dump(sprintf('I would have required: (%s)', 'lib/'.$lib.'.lib.php'));
                    $this->$memberName = new $lib();
                } else {
                    continue;
                }
            }
        }
    
        public function __set($name, $value)
        {
            $this->dynamicMembers[$name] = $value;
        }
    
        public function __get($name)
        {
            if (array_key_exists($name, $this->dynamicMembers)) {
                return $this->dynamicMembers[$name];
            }
        }
    }
    
    $myClass = new MyClass();
    
    $myClass->loadLibs();
    
    var_dump((string) $myClass->lib_strings);
    var_dump($myClass->lib_strings);
    
    exit;
    

    — Edit #1 —

    After closer inspection of your code, it appears you are trying to create an autoloader coupled with a lazy-load pattern. Lazy-loading works well in some situations, but keep in mind that it will be difficult for calling code to set specific member attributes of the objects being instantiated, if this is a non-issue, then disregard.

    Also, I would recommend reading up on PHP’s built-in spl_autoload capabilities.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.