Jump to content

Recommended Posts

Issue:

Create an object that gives 1 entry point for the creation of all objects, and act's as the API for all object calls.

 

Problem:

All (NON-Singleton) objects that need a __construct method must have  ONLY optional parameters.

 

Ex:

class MySQL {
     private $a,$b,$c;
     
     public function __construct($a,$b,$c) {
          $this->a=$a;
          $this->b=$b;
          $this->c=$c;
     }

     public function reportVars() {
          echo("Vars: ".$a."<br />".$b."<br />".$c."<br />");
     }
}

$args = array("This is a","This is b","This is c");

Factory::_("MySQL",$args)->reportVars(); //This call will fail because the parameters of MySQL->__constuct() are NOT optional

 

Where the factory not only creates the object "MySQL" but it also will pass a given set of parameters to MySQL's constructor. This can almost be accomplished with

call_user_func_array

the only issue is that you have to create the object prior to using

call_user_func_array

which means the parameter's of MySQL's constructor must be optional.

Construct params are optional

class foo {
public function __construct($params = array()) {
   foreach($params as $key => $val) {
      $this->$key = $val;
   }
}
}

$x = new foo(array('a' => 1, 'b' => 2));

Perhaps your factory could use Reflection?

 

$ref = new ReflectionClass('Fruits');
$foo = $ref->newInstanceArgs(array('apple', 'banana', 'cherry'));

 

 

P.S. That essentially calls new Fruits('apple', 'banana', 'cherry');

class MySQL_Instantiable {
     public function __construct($user, $pass, $location, $db) {
          // do some work
     }
}

class MySQL_Singleton {
     private static $instance;

     private function __construct() {}

     public static function getInstance($user, $pass, $location, $db) {
          //make a singleton
     }
}

 

The issue is the way these 2 objects are created.

Respectively:

 

$object = new MySQL_Instantiable();

$singleton = MySQL_Singleton::getInstance();

 

They are created two totaly different ways.

Also the end result being:

 

$args = array("User","Password","Localhost","db1");
Factory::_("ObjectName",$params)->query("SELECT * FROM table")->fetchRows();
// or
Factory::_("ObjectName",$params)->query("SELECT * FROM table");
Factory::_("ObjectName")->fetchRows();

 

The factory object will interface directly with the registry

so there will be a global placeholder for the object you are

creating. Unless it is a singleton therefore does not need

the be stored in the registry.

 

The request -> response for that call would be as follows:

 

1.) Factory checks to see if "ObjectName" exists already

    1a.) if so return it as is

    1b.) if not call it's __construct or getInstance()

2.) Pass $args to the method in the correct order

3.) return the current instance of the object your working with

4.) call the remaining chained methods

Again, no matter how the object is created either instantiated via new or from a static method returning an object I cannot see where your issue lies with using params required (or optional) in the construct.

The factory needs to be able to handle the creation of both types of objects

and needs to be able to handle method calls during creation that have required

parameters. The reflection class will work for objects that can be created via the

"new" keyword, but will not work for singleton objects, therefore it will not work

in passing required parameters to a singleton object.

The singleton pattern should return an object! Your objective is confusing. Take the following:

<?php
class base {
public $params;

function set($key, $val) {
	$this->params[$key] = $val;
}

function get($key) {
	return $this->params[$key];
}

function createParams($array = array()) {
	foreach($array as $key => $val) {
		$this->set($key, $val);
	}
}
}



class foo extends base {
public function __construct($params) {
	$this->createParams($params);
}
}


class bar extends base {
public function __construct($params = array()) {
	$this->createParams($params);
}
}



class factory {
public static function getInstance($object, $params = array()) {
	return new $object($params);
}
}


$x = factory::getInstance('foo', array(1,2,3));
print $x->get(0);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.