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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:45:30+00:00 2026-06-04T19:45:30+00:00

I have a language class with public data members: class language { public $error

  • 0

I have a language class with public data members:

class language {
    public $error = "There was an error";
}

Then I have many different classes that uses this class. How can I access this class from within other classes? You could of course instantiate an object of it inside all the classes that uses it, but I suppose that would drastically decrease the performance, if I were to do that with multiple classes.

I do only need one object, since I don’t change them anyway. So, I’ve done

$language = new language();

class name {
    private $language;

    public function __construct() {
        global $language;
        $this->language = $language;
    }


    public function method() {
        mysql_query(...) or die($this->language->error);
    }
}

Is this a good way to do it? Now I have to instantiate it in every “mother” script. And I suppose by doing __construct() I’m only passing a refrence, since I don’t use the clone word?

  • 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-04T19:45:32+00:00Added an answer on June 4, 2026 at 7:45 pm

    I do only need one object

    This is the Singleton pattern.

    Here is a quick example of a way to have only one instance of your Language class:

    class Language {
      public $error = "text";
    
      private static $instance = null;
    
      public static function getInstance() {
        if (self::$instance == null)
          self::$instance = new self();
        return self::$instance;
      }
    
      private function __construct() {}
    }
    
    $lang = Language::getInstance();
    $text = $lang->error;
    

    You can also use dependency injection:

    interface ILanguage {
      public function getError();
    }
    
    class Language implements ILanguage {
      private $error = "error";
      public function getError() {
        return $this->error;
      }
    }
    
    class name {
      private $language;
    
      public function __construct(ILanguage $lang) {
        $this->language = $lang;
      }
    
       public function method() {
         mysql_query(...) or die($this->language->getError());
       }
    }
    
    $language = new Language();
    $n = new name($language);
    

    This example of dependency injection is called constructor injection.

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

Sidebar

Related Questions

This semester in university I have a class called Data Structures, and the professor
I have this code: public class VoiceActivity extends Activity implements OnClickListener { private TextView
For instance I have this bit of code public class ProductService{ private IProductDataSource _dataSource
Ok so at work we have discovered that this method to fetch related data
I have this class used in the API demos: public class TextToSpeechActivity extends Activity
Okay so I have started a new language in class. We are learning Scheme
I have 3 models class User < ... belongs_to :language has_many :posts end class
Is there a way to have Expression Language (EL) expressions included JavaScript files be
I have an English language forum site written in perl that is continually bombarded
TL; DR: I'm new to this language and have no idea what I'm doing

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.