Рубрики
Uncategorized

Пример анализа одноэлементного шаблона PHP [предотвращение наследования, операция по предотвращению клонирования]

Автор оригинала: David Wong.

Эта статья иллюстрирует шаблон PHP-синглтона примерами. Поделитесь для вашей справки следующим образом:

php
// Single column mode
//// 1. General Class
// class singleton{
// }
// $s1 = new singleton();
// $s2 = new singleton();
//// Note that two variables are equal when they are the same object
// if ($s1 === $s2) {
// echo'Is an object';
// }else{
// echo'Is not an object';
// }
//// 2. Blocking new operation
// class singleton{
//   protected function __construct(){}
// }
// $s1 = new singleton();//PHP Fatal error: Call to protected singleton::__construct()
//// 3. Leave an interface to the new object
// class singleton{
//   protected function __construct(){}
//   public static function getIns(){
//     return new self();
//   }
// }
// $s1 = singleton::getIns();
// $s2 = singleton::getIns();
// if ($s1 === $s2) {
// echo'Is an object';
// }else{
// echo'Is not an object';
// }
//// 4. getIns First Judgement Example
// class singleton{
//   protected static $ins = null;
//   private function __construct(){}
//   public static function getIns(){
//     if (self::$ins === null) {
//       self::$ins = new self();
//     }
//     return self::$ins;
//   }
// }
// $s1 = singleton::getIns();
// $s2 = singleton::getIns();
// if ($s1 === $s2) {
// echo'Is an object';
// }else{
// echo'Is not an object';
// }
// // inheritance
// class A extends singleton{
//   public function __construct(){}
// }
// echo '
'; // $s1 = new A(); // $s2 = new A(); // if ($s1 === $s2) { // echo'Is the same object'; // }else{ //Echo 'is not the same object'; // } //// 5. Preventing inheritance by modifying permissions // class singleton{ // protected static $ins = null; //// if a method is added with final, the method cannot be overwritten. If a class is added with final, the class cannot be inherited. // final private function __construct(){} // public static function getIns(){ // if (self::$ins === null) { // self::$ins = new self(); // } // return self::$ins; // } // } // $s1 = singleton::getIns(); // $s2 = singleton::getIns(); // if ($s1 === $s2) { // echo'Is the same object'; // }else{ //Echo 'is not the same object'; // } // // inheritance // // class A extends singleton{ // // public function __construct(){} // // } // //Cannot override final method singleton::__construct() // echo '
'; // $s1 = singleton::getIns(); // $s2 = clone $s1; // if ($s1 === $s2) { // echo'Is the same object'; // }else{ //Echo 'is not the same object'; // } // 6. Prevent clone class singleton{ protected static $ins = null; //Method with final cannot be overwritten, class with final cannot be inherited final private function __construct(){} public static function getIns(){ if (self::$ins === null) { self::$ins = new self(); } return self::$ins; } // Clone blockade final private function __clone(){} } $s1 = singleton::getIns(); $s2 = clone $s1; //Call to private singleton::__clone() from context if ($s1 === $s2) { Echo'Is the same object'; }else{ Echo'Is not the same object'; }

Больше читателей, интересующихся контентом, связанным с PHP, могут ознакомиться с темами этого сайта: Введение в объектно-ориентированное программирование Php, Введение в навыки работы с массивами PHP, Введение в базовую грамматику PHP, Краткое описание работы с PHP и использования операторов, Краткое описание использования строк Php, Введение в работу с базой данных php+mysql и Общие операции с базами данных php. Резюме навыков

Я надеюсь, что эта статья будет полезна для программирования на PHP.

Оригинал: “https://developpaper.com/example-analysis-of-php-singleton-pattern-inheritance-prevention-clone-prevention-operation/”