I use the registry:
class Registry { public static $objects = array(); protected static $instance; /*protected function __construct() { //метод для автодобавления классов в файл конфигурации global $config; foreach ($config['pages'] as $name => $obj) { self::$objects[$name] = new $obj; } }*/ public static function instance () { if(self::$instance === null) { self::$instance = new self; } return self::$instance; } public function __set($name, $value) { if (!isset(self::$objects[$name])) { self::$objects[$name] = new $value; } } public function __get($name) { if (is_object(self::$objects[$name])) { return self::$objects[$name]; } } } in order to use the necessary classes at the required time and the necessary place of the code, then the loader of all additional classes from the libs and models folders:
class LoadClasses { private $app; public function __construct() { return $this->app = Registry::instance(); } public function LoadLibs ($dir) { if($handle = opendir($dir)){ while(false !== ($file_dir = readdir($handle))) { if($file_dir != "." && $file_dir != ".."){ $data = file_get_contents($dir.'/'.$file_dir); preg_match_all('/class [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $data, $matches); if (!empty($matches[0])) { foreach ($matches[0] as $val) { $className = 'lib_'.substr($val, 6); $this->app->$className = substr($val, 6); } } } } } } public function LoadModels ($dir) { if($handle = opendir($dir)){ while(false !== ($file_dir = readdir($handle))) { if($file_dir != "." && $file_dir != ".."){ $data = file_get_contents($dir.'/'.$file_dir); preg_match_all('/class [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $data, $matches); if (!empty($matches[0])) { foreach ($matches[0] as $val) { $classNameMods = 'mod_'.substr($val, 6); $this->app->$classNameMods = substr($val, 6); } } } } } } } $loader = new LoadClasses(); what is the actual problem, all the files with cash registers from the libs directory are successfully processed and entered into the registry and after that I can work with them when I need it and where, but the problem with classes from the models directory is that they display the result even if you don’t refer to I call class loaders for this class in the index file:
$loader->LoadLibs(WWW.'/libs'); $loader->LoadModels(WWW.'/models'); example class from libs directory:
class validateForms { public function FormData ($data) { echo $data . ' Fuck! '; } } An example of a class from the catalog models:
class MainModel extends Model { public function mainModel () { echo __CLASS__." model! "; } } Why classes from models display the result even if they are not accessed ??? every day by themselves!