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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:56:13+00:00 2026-05-25T17:56:13+00:00

Its’ hard to explain what i mean so here an example. class mysql {

  • 0

Its’ hard to explain what i mean so here an example.

class mysql {

    public function __construct(){
        //connect to db and select table
    }
    ....
}


class Index Extends perent_class {

    public $mysql;

    public function no_mysql_required(){

        //this function might be called and no mysql is required 

    }

    public function mysql_required(){..}  // this needs mysql. 
    public function mysql_required2(){..} // this needs mysql.
    public function mysql_required3(){..} // this needs mysql.

}

Option 1:

$this->mysql = new mysql();

add this to all function that need the connection to mysql, but two of these function could be called and that would creates two connection to mysql which is bad.


Option 2:

if( !is_object($this-mysql) ) $this->mysql = new Mysql();

This will only create one mysql object at once and solves the problem, but it creates very repetitive code in the function(s). Creating & Calling a function $this->start_mysql() would be repetitive too.


So what i really want is whenever i call $this->mysql->function(); even tho its not created yet, to create the “$this->mysql = new Mysql” somehow in the Index perent_class automatically and once created, not to recreate it when a new function call is made. I might be doing this wrong and if there a better solution to what I’m doing, please post it.

Thank you.

  • 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-25T17:56:13+00:00Added an answer on May 25, 2026 at 5:56 pm

    If I understood your question correctly, it seems like you’re wanting to use the ‘Singleton’ design pattern. This allows you to only create one instance of a class; attempting to create a second instance only returns the original instance. Here’s a code extract:

    <?php
    
    class mysql {
      private static $mysql;
    
      public $testProperty;
    
      private function __construct() { } // Declared private so cannot instantiate objects in client code.
    
      public static function instance() {
        if (!isset(self::$mysql)) {
          self::$mysql = new self();
        }
        return self::$mysql;
       }
     }
    
    ?>
    

    By using this pattern, a call to mysql::instance(); will return a new instance of the mysql object if one has not been created. If it has been created, it will return the same instance. Thus, you only ever have one instance of the object. Test it out by making several calls to mysql::instance():

    $instance1 = mysql::instance();
    $instance1->testProperty = 'abc';
    var_dump($instance1->testProperty);
    
    $instance2 = mysql::instance();
    var_dump($instance2->testProperty);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Its a bit of a tough one to explain really. this is it: <div
Its quite possible I'm missing something obvious here; I hope so. I have been
Its possible I am just really really thick. However, looking over the SDK for
its out of question that a dao will not hold any state. however, for
Its a simple config app with 4 checkboxes and 5 textboxes, and all values
Its a .vbproj and looks like this <Project DefaultTargets=Build xmlns=http://schemas.microsoft.com/developer/msbuild/2003> <PropertyGroup> <ProjectGuid>15a7ee82-9020-4fda-a7fb-85a61664692d</ProjectGuid> all i
If its dead, is there a successor? For those who didn't know: The Network
In its enthusiasm to stemm tokens into lexemes , PostgreSQL Full Text Search engine
Sometimes its necessary to code in AS2 to target Flash Player 8, so we're
When developing whether its Web or Desktop at which point should a developer switch

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.