Derived class database simple interest mode
Release: 2016-07-25 08:47:30
Original
1023 people have browsed it
Derived class database simple interest mode
- //The problem to be solved is calling the class multiple times in one method
- //Multiple calls to the same parent class
- class Pdoo {
- public function __construct(){}
- //This It is a database class
- function select($name) {
- echo "authentic" . $name;
- }
- }
- class Conn {
- static $db;
- private function __construct() {
-
- }
-
- private function __clone( ) {
-
- }
- //Returns the database connection instead of the Base class
- public static function getInstance() {
- if (self::$db == null) {
- self::$db = new Pdoo ( );
- }
- return self::$db;
- }
-
- //This method is invalid
- function select($name) {
- echo $name;
- }
- }
- class Db {
- static $db;
- static $instanceInternalCache;
- private function __construct() {
- //The initialization has nothing to do with connecting to the database
- }
-
- private function __clone() {
- }
- //Cannot be instantiated here
- public static function getDb( ) {
-
- }
- //The solution here is that if A is called multiple times in the same method, it will not be instantiated multiple times
- //It cannot be solved that multiple derived classes are instantiated, that is, as many derived class databases are connected as many times
- public static function getInstance($model) {
- if (self::$instanceInternalCache [$model] == NULL) {
- self::$instanceInternalCache [$model] = new $model ();
- }
-
- return self ::$instanceInternalCache [$model];
- }
-
- function select($name) {
- Conn::getInstance ()->select ( $name );
- }
- }
-
- class A extends Db {
-
- function s($name) {
- $this->select($name);
- }
-
- public static function instance() {
- return parent::getInstance ( __CLASS__ );
- }
- }
-
- class B extends Db {
-
- function s($name) {
- $this->select($name);
- }
- public static function instance() {
- return parent::getInstance ( __CLASS__ );
- }
- }
- class Main {
-
- public function t() {
- A::instance ()->select ( "1" );
- A::instance ()->select ( "11" );
-
- B::instance ()- >select ( "2" );
-
- }
- }
- $t = new Main ();
- $t->t ();
Copy code
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31