Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PHP MVC from the scratch - How to connect these classes?
- <?php
- // filename: "config.php"
- // another includes or requires here
- class Config
- {
- // begin data section
- public static $UserName;
- public static $UserPassword;
- // end data section
- // begin singleton section
- private static $_instance;
- public static function getInstance()
- {
- if (!self::$_instance instanceof self)
- {
- self::$_instance = new self;
- }
- return self::$_instance;
- }
- // end singleton section
- // start session section
- public static function SaveSession()
- {
- $_SESSION['config_username'] = Config::$UserName;
- $_SESSION['config_password'] = Config::$UserPassword;
- }
- public static function RestoreSession()
- {
- Config::$UserName = $_SESSION['config_username'];
- Config::$UserPassword = $_SESSION['config_password'];
- }
- // end session section
- } // class Config
- ?>
- <?php
- // filename: "index.php"
- include ("config.php");
- // another includes or requires here
- class Application
- {
- public static function main()
- {
- // prepare session variables
- session_start();
- // prepare singletons here
- $instance = Config::getInstance();
- // this code its an example
- $instance->UserName = "johndoe";
- $instance->UserPassword = "123";
- $instance->SaveSession();
- // all the page construction goes here
- // ...
- }
- } //
- // program starts here:
- Application::main();
- ?>
- <?php
- // filename: "customers.php"
- include ("config.php");
- // another includes or requires here
- class Application
- {
- public static function main()
- {
- // copy data from session to singletons
- $instance->RestoreSession();
- // all the page construction goes here
- // ...
- }
- } //
- // program starts here:
- Application::main();
- ?>
Add Comment
Please, Sign In to add comment