Singleton class explained in detail
Singleton Design pattern in php explained
1.Most frequently asked question in interview (PHP , java or C++) .
2.Singleton design pattern is a design pattern in which we create a only one instance of a class in our application. To achieve this we put conditions in a class so that only one instance can be created .
3.Factory methods are responsible for creating and returning an instance of a class.Here we are going to use factory method to create and return an instance of class.
4.Here we define constructor with private access specifier. As we all know that private method can be access from within class not from outside the class.In the same way if define construct as private in the class then we can not create object by new operator outside the class.
5.We define a factory method for creating and returning an instance of the class within the class.Check whether a instance is created or not if not then created instance and return otherwise return already created instance of the class.
6.Inspite of applying all the above conditions we can create another instance of this class.To avoid this we define __clone (magic method called while creating the clone of the object) as private so that this method could not be called from outside the class.
7. Final outcome will be
class xyz{
private static $instance = null;
private function __construct(){
echo "Private constructor is called here";
}
private function __clone(){
echo "Private magic method clone is called here";
}
public static function getInstance(){
if(!isset(self::$instance)){
self::$instance = new xyz();
}
return self::$instance;
}
/*public static function getInstanceClone(){
return clone self::$instance;
}*/
}
$xyzObj = xyz::getInstance();
$xyzObj2 = xyz::getInstance();
$xyzObj3 = xyz::getInstance();
//$xyzClone = clone $xyzObj;;
var_dump($xyzObj);
var_dump($xyzObj2);
var_dump($xyzObj3);
//var_dump($xyzClone);
@Codeguru-OnlineTutor
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Singleton class explained in detail», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.