Derived class database simple interest mode

WBOY
Release: 2016-07-25 08:47:30
Original
1023 people have browsed it
Derived class database simple interest mode
  1. //The problem to be solved is calling the class multiple times in one method
  2. //Multiple calls to the same parent class
  3. class Pdoo {
  4. public function __construct(){}
  5. //This It is a database class
  6. function select($name) {
  7. echo "authentic" . $name;
  8. }
  9. }
  10. class Conn {
  11. static $db;
  12. private function __construct() {
  13. }
  14. private function __clone( ) {
  15. }
  16. //Returns the database connection instead of the Base class
  17. public static function getInstance() {
  18. if (self::$db == null) {
  19. self::$db = new Pdoo ( );
  20. }
  21. return self::$db;
  22. }
  23. //This method is invalid
  24. function select($name) {
  25. echo $name;
  26. }
  27. }
  28. class Db {
  29. static $db;
  30. static $instanceInternalCache;
  31. private function __construct() {
  32. //The initialization has nothing to do with connecting to the database
  33. }
  34. private function __clone() {
  35. }
  36. //Cannot be instantiated here
  37. public static function getDb( ) {
  38. }
  39. //The solution here is that if A is called multiple times in the same method, it will not be instantiated multiple times
  40. //It cannot be solved that multiple derived classes are instantiated, that is, as many derived class databases are connected as many times
  41. public static function getInstance($model) {
  42. if (self::$instanceInternalCache [$model] == NULL) {
  43. self::$instanceInternalCache [$model] = new $model ();
  44. }
  45. return self ::$instanceInternalCache [$model];
  46. }
  47. function select($name) {
  48. Conn::getInstance ()->select ( $name );
  49. }
  50. }
  51. class A extends Db {
  52. function s($name) {
  53. $this->select($name);
  54. }
  55. public static function instance() {
  56. return parent::getInstance ( __CLASS__ );
  57. }
  58. }
  59. class B extends Db {
  60. function s($name) {
  61. $this->select($name);
  62. }
  63. public static function instance() {
  64. return parent::getInstance ( __CLASS__ );
  65. }
  66. }
  67. class Main {
  68. public function t() {
  69. A::instance ()->select ( "1" );
  70. A::instance ()->select ( "11" );
  71. B::instance ()- >select ( "2" );
  72. }
  73. }
  74. $t = new Main ();
  75. $t->t ();
Copy code


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template