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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:09:52+00:00 2026-06-06T09:09:52+00:00

I’ve created a custom library using singleton pattern. Why by this way? because I

  • 0

I’ve created a custom library using singleton pattern. Why by this way? because I need to be able to call my functions without $this-> reference, for example, I could execute the code below:

function foo() {
    MyLibrary::instance()->foo();        
}

then I could call my functions in controllers like this:

function foo();

instead of

$this->mylibrary->foo();

I am in trouble because CodeIgniter try to instantiate my library when the best way to do is “read” the static instance.

Why I need to do this?

My custom library must register “hooks” from external php files located in /plugins directory outside of application folder. Look:

  • /application
  • /plugins
  • /plugins/helloworld.php

This is my Loader library:

class Loader{

    public static $instance;
      protected $hooks;

    private function __construct($params = array()){
        $this->hooks= array();
        $this->includePlugins();
    }

    public static function instance()
        {
            if (!self::$instance)
            {
                self::$instance = new Loader();
            }

            return self::$instance;
        }

    public function includePlugins()
    {
        include_once "/plugins/helloworld.php";  
    }

    public function do_action($hook= "after")
    {
        foreach ($this->actions[$hook] as $function)
        {
            call_user_func_array($function, array());
        }

    }

    public function add_action($hook, $function, $priority)
    {
        if ($hooks !== false)
        {
        $this->actions[$hook][] = $function;
        }
        else
        {
        echo "hook name is unavalaible";
        }
    }

}



/**
 * Functions using the Singleton instance !!!
*/

function do_action($hook)
{
    Loader::instance()->do_action($hook);
}

function add_action($hook, $function, $priority)
{
    Loader::instance()->add_action($hook, $function, $priority);
}

No, my helloworld.php (plugin) looks like this:

add_action('right_here', 'show_message', 11);
function show_message()
{
    echo "hello world!";
}

Finally, I could call my the function do_action in my controller/view in order to print the message, like this:

do_action('right_here');

Note that all my important functions are called by a global function that allow me to use the singleton instance. But I am experiencing two issues:

  1. Codeigniter needs that my contruct be public in order to
    instantiate. Here I need to take my singleton.
  2. If I let it public, several instances will be created and the library
    will no longer works as expected.
  • 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-06T09:09:53+00:00Added an answer on June 6, 2026 at 9:09 am

    I’ve solved the problem 🙂

    In order to create a singleton library you must:

    • Include your library at system/core/Codeigniter.php like this:
      require_once(“/path/to/library.php”);

    • Make your instance return with & symbol, like this:

    Singleton:

    public static function &instance()
    {
        if (!self::$instance)
            {
                self::$instance = new Loader();
            }
    
            return self::$instance;
    }
    
    • Ready to uses your functions as well. Also, you can include th CI
      object in you library.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.