Class for automatically connecting php classes
As always, surf the internet, in search of interesting solutions, I came across this class of automatic connection of classes, made in php. I won't write for a long time., I'll just share it with my blog readers., who programs, will understand.
All work on creating and connecting a class consists of several stages:
- We create 2 index.php and autoload.php files.
- We need to insert our class into the autoload.php file.
- In the index.php file we will connect and try the work of the class.
- Next to the index.php and autoload.php files, you need to create folders
- controller
- model
- view
Class code for the autoload.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php class autoloader { public static $loader; public static function init() { if(self::$loader == NULL) { self::$loader = new self(); } return self::$loader; } public function __construct() { spl_autoload_register(array($this, 'controller')); spl_autoload_register(array($this, 'model')); spl_autoload_register(array($this, 'view')); } public function library($class){ set_include_path(ROOT_DIR . '/lib'); spl_autoload_extensions('.php'); spl_autoload($class); } public function controller($class){ set_include_path(ROOT_DIR . '/controller'); spl_autoload_extensions('.php'); spl_autoload($class); } public function helper($class){ set_include_path(ROOT_DIR .'/helper'); spl_autoload_extensions('.php'); spl_autoload($class); } public function view($class){ set_include_path(ROOT_DIR .'/view'); spl_autoload_extensions('.php'); spl_autoload($class); } public function model($class){ set_include_path(ROOT_DIR .'model'); spl_autoload_extensions('.php'); spl_autoload($class); } } |
1 2 3 |
define("ROOT_DIR",dirname(__FILE__).'/'); require_once "autoload.php"; $autoloader = new autoloader(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php class cconfig { private $params; function get($param1, $param2) { return $this->params[$param1][$param2]; } function __construct() { $this->params = array ( 'database' => array ( 'server' => 'localhost', 'login' => 'mylogin', 'password' => 'pass', 'dbname' => 'db', 'charset' => 'utf8' ), 'extra' => array ( 'adress' => 'siteadress', 'adminemail' => 'info@wp-admin.com.ua' ), 'pics' => array ( 'small_w' => '120', 'small_h' => '120', 'small_path' => 'pics/small/', 'big_w' => '250', 'big_h' => '335', 'big_path' => 'pics/big/', 'max_pics' => '6', 'objpicbig_w' => '541', 'objpicbig_h' => '335', 'objpicbig_path' => 'pics/objbig/', 'objpicmini_w' => '51', 'objpicmini_h' => '46', 'objpicmini_path' => 'pics/objmini/' ), 'other' => array ( 'groupid' => '15', 'groupid' => '4', 'groupid' => '3' ), ); } function __destruct() { } } ?> |
1 2 3 4 5 |
define("ROOT_DIR",dirname(__FILE__).'/'); require_once "autoload.php"; $autoloader = new autoloader(); $cConfig = new CConfig; echo $cConfig->get('database', 'login'); |
This design is done once during development and can be used, so to save your time, I carefully packed everything into an archive and post it on my blog:
If this code was useful to anyone, write in the comments, I will be glad to receive good feedback on the work done.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.