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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:27:00+00:00 2026-05-12T06:27:00+00:00

I am coding my first object factory, and I am wondering if there any

  • 0

I am coding my first object factory, and I am wondering if there any any drawbacks to passing one object into another when creating the second object, and then saving the first object within the second for use later?

Example:

class main
{
    public $connection = array();

    // various other useful functions that should be loaded on every page here
    // these other functions would be convenient to have avail for 
    // new objects in different classes

    function dbconnect($db) {
        if(!isset($this->connection[$db])) {
            switch($db) {
                case "users":
                    $connection = @mysql_connect(info,info,info);
                    $db_select = @mysql_select_db(info,$connection);
                    $this->connection[$db] = $connection;
                break;

                case "ads":
                    $connection = mysql_connect(info,info,info);
                    $db_select = mysql_select_db(info,$connection);
                    $this->connection[$db] = $connection;
                break;

                default:
                    die($db . " is not a recognized database.");
                break;
            }
        }

        return $this->connection[$db];
    }

    // note how i pass $this into the new object in this function
    function create($class,$params=null) { 
        if(!class_exists($class) && !$this->load($class)) {
                die($class . " is not a recognized class.");
        }else{
                return new $class($this,$params);
        }
    }

    function load($file) {
        $file = $_SERVER['DOCUMENT_ROOT'] . "/classes/" . $file . ".class.php";
        if(file_exists($file)) {
                return require_once($file);
        }else{
                return false;
        }
    }
}

class secondary {

    private $connection;
    private $mainObj;
    private $params;

    function __construct($mainObj,$params) {

        $this->mainObj = $mainObj;
        $this->params = $params;

        $this->connection = $mainObj->dbconnect('users');

    }
}

As you see here, any class created by the factory conveniently has the ability to connect to the MySQL db if the class requires it (and other common/useful functions) … without having to use globals, or pass many things in.

Is this a stable way? I am new, and could use some good theory/schooling on why this is wrong – if it is.

  • 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-12T06:27:00+00:00Added an answer on May 12, 2026 at 6:27 am

    Yes, it is a pretty stable way. But I would like to see type hinting in the methods taking outer instances as parameters, which would make your classes typed stronger.

    Example;

    class Alob {
        public function aMethod(**Blob** $outerObject) {
            ...
        }
    }
    
    class Blob {
        ...
    }
    

    This will throw a fatal error if Alob::aMethod() is called with another type than Blob.

    I would also like to see more accessor keywords for defining visibility, hide your class privates with ‘private’ and so on.

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

Sidebar

Related Questions

The coding is done using VS2008 There are two divs in my page namely
First off, there's a bit of background to this issue available on my blog:
I'm coding a poker hand evaluator as my first programming project. I've made it
I am new to object oriented programming and writing some of my first classes
I have some coding practices involving delegates that I'm not sure of. First of
When coding web services, how do you structure your return values? How do you
In coding a traditional MVC application, what is the best practice for coding server-side
When coding, what in your experience is a better approach? Break the problem down
When coding, what is a good rule of thumb to keep in mind with
Our coding standards ask that we minimise the use of C# var (suggests limiting

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.