doing this....
Code: Select all
require 'one.class.php';
require 'two.class.php';
require 'three.class.php';
$one = new one();
$two = new two();
$three = new three();Moderator: General Moderators
Code: Select all
require 'one.class.php';
require 'two.class.php';
require 'three.class.php';
$one = new one();
$two = new two();
$three = new three();Code: Select all
<?php
$error = new error();
$mysql = new MySQL();
$config = new Config();
?>Code: Select all
<?php
$myapp = new MyApp;
$myapp->initialize();
// That method would set up all the objects
if (!$myapp->mysql->connected) {
$myapp->error->show_error($myapp->mysql->last_error_message);
}
?>Code: Select all
<?php
// set include path to include the path to Everah's parent directory. Not Everah itself though...
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Everah/parent/directory');
// Use static methods - i.e. globally accessible from all classes
class Everah_Locator
{
private static $registry = array();
// ...
public static function get($class)
{
// re: Class Name is the relative Path replacing / with _ (PEAR convention)
$path_components = explode('_', $class);
$class_path = implode(DIRECTORY_SEPARATOR, $path_components) . '.php';
$registry_key = end($path_components);
if(array_key_exists($registry_key ,self::$registry))
{
return self::$registry[$registry_key];
}
// else
require_once $class_path;
$object = new $class;
self::$registry[$registry_key] = $object;
return $object;
}
// ...
}Code: Select all
// usage:
require '/path/to/Everah/Locator.php';
// Everah_Mysql is stored in the Mysql.php file at ./Everah/Mysql.php
$mysql = Everah_Locator::get('Everah_Mysql');
// you now have the Everah_Mysql object, which was required, instantiated and cached
// Further get('Everah_Mysql') calls will return the cached object reference
$mysql2 = Everah_Locator::get('Everah_Mysql');
// mysql and mysql2 contain a reference to the same object as cached in LocatorWhy? If you will use those three classes it that context then that is the cleanest and most concise way to do it. I would like to know what feels 'dirty' about it?scottayy wrote:doing this....
just feels too "dirty" and I don't like doing it. creates overhead.Code: Select all
require 'one.class.php'; require 'two.class.php'; require 'three.class.php'; $one = new one(); $two = new two(); $three = new three();
NPWOW! Thanks Maugrim. That gives me a ton to think about tomorrow when I go through my structure. I truly appreciate the time you put into the explanation. Thanks again.
What overhead? You just described the only method of instantiating an object...just feels too "dirty" and I don't like doing it. creates overhead.