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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:17:22+00:00 2026-06-01T01:17:22+00:00

I have an object of some class that obeys the singleton pattern. I need

  • 0

I have an object of some class that obeys the singleton pattern. I need to initialize it in one file and then use it in others. I don’t know how to do this, here is what I tried :

//myClass.php
class myClass
{
    private static $instance = null;

    private function __construct($args)
    {
        //stuff
    }

    public function Create($args)
    {
        self::$instance = new myClass($args);
        return self::$instance;
    }

    public function Get()
    {
        return self::$instance;
    }
}

//index.php
<?php
require_once('myClass.php');
$instance = myClass::Create($args);
?>
<a href="test.php">Test Me!</a>

//test.php
echo(is_null(myClass::Get())); //displays 1

So the problem is that from test.php, myClass::get() always returns null!

I have also tried to store the instance in the $_SESSION, which gives me the same result. Can you please point me in the right direction?

  • 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-01T01:17:23+00:00Added an answer on June 1, 2026 at 1:17 am

    You should include file with the class difinition in each file where it used (and it should be included before it will in use).

    <?php // filename: test.php
    include_once("myClass.php");
    $oClassInstance = myClass::Get();
    
    var_dump($oClassInstance);
    

    BTW

    You don’t need to define those two methods Create and Get. You can create only one method called getInstance:

    // only one instance of the class
    private static $_oInstance = null;
    
    public static function getInstace()
    {
        if (!self::$_oInstance)
        {
            self::$_oInstance = new self();
        }
    
        return self::$_oInstance;
    }
    

    And then you can use it like:

    <?php // filename: index.php
    include_once("myClass.php");
    
    // if instance does not exist yet then it will be created and returned
    $oClass = myClass::getInstace();
    
    <?php // filename: test.php
    include_once("myClass.php");
    
    // the instance already created and stored in myClass::$_oInstance variable
    // so it just will be returned
    $oClass = myClass::getInstance();
    

    UPD

    If you have to put some arguments into constructor just use predefined arguments:

    private function __construct($aArg)
    {
        // this code will be launched once when instance is created
        // in the any other cases you'll return already created object
    }
    
    public static function getInstance($aArgs = null)
    {
        if (!self::$_oInstance)
        {
            self::$_oInstance = new self($aArgs);
        }
    
        return self::$_oInstance;
    }
    

    ANSWER

    Sorry that you have to scroll a few screens to find this =)))
    The reason why you can’t use myClass::Get() in you context is that you have 2 scripts that means – two different programs.
    Singleton should be used within a single application (one script).

    So in your case, correct usage will be module system:
    – index.php
    – main.php
    – test.php

    // file: index.php
    include_once "myClass.php"
    
    $module = $_GET["module"];
    include_once $module ".php";
    
    // file: main.php
    $oClass = myClass::Create($someArgs);
    var_dump($oClass); // you'll see you class body
    
    // file: test.php
    $oClass=  myClass::Get();
    var_dump($oClass); // you'll see the same class body as above
    

    And your links will be:

    • index.php?module=main
    • index.php?module=test
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have some class objects, each with three properties. Once one class object
Let's say I have some class called loopObject and I initialize every object through
I have some object classes that use inheritance. It seems that I can only
I want to have one object of some .NET class used by multiple processes
I have a class that holds some constants and will receive an object literal
I have a class object that stores some properties that are lists of other
Assume some domain and view objects (that have no common base class) public class
I have some object that is instantiated in code behind, for instance, the XAML
I need to have some object hanging around between two events I'm interested in:
I am using C# 2.0 with Nunit Test. I have some object that needs

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.