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 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

Currently I'm hard coding another object directly when needed and I'd like the ability
Question First Is there an elegant solution in C++ to prevent one from having
I need to map this data into one Core Data object (EventList). { eventList:[
I'm finding myself coding C3 for the first time, and using Visual Studio for
This is my first time on StackOverflow though I read Coding Horror quite often.
I initially used MD5 when i first started out coding to hash user passwords.
I wrote my first program almost fifty years ago (yes, coding is still a
First off, I'm a complete beginner at C++. I'm coding something using an API,
sorry about the funky title, I was having trouble coming up with one. First
I'm coding a poker hand evaluator as my first programming project. I've made it

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.