new self ();는 무엇을합니까? PHP에서 의미합니까?
다음과 같은 코드는 본 적이 없습니다.
public static function getInstance()
{
if ( ! isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
과 동일 new className()
합니까?
편집하다
클래스가 상속인 경우 어떤 클래스를 가리 킵니까?
self
기록 된 클래스를 가리 킵니다.
따라서 getInstance 메서드가 클래스 이름 MyClass
에있는 경우 다음 줄은 다음과 같습니다.
self::$_instance = new self();
다음과 같은 작업을 수행합니다.
self::$_instance = new MyClass();
편집 : 주석 후 몇 가지 추가 정보.
서로 확장하는 두 개의 클래스가있는 경우 두 가지 상황이 있습니다.
getInstance
자식 클래스에 정의되어 있습니다.getInstance
부모 클래스에 정의되어 있습니다.
첫 번째 상황은 다음과 같을 것입니다.
class MyParentClass {
}
class MyChildClass extends MyParentClass {
public static function getInstance() {
return new self();
}
}
$a = MyChildClass::getInstance();
var_dump($a);
여기에서 얻을 수 있습니다.
object(MyChildClass)#1 (0) { }
어떤 의미 self
수단을 MyChildClass
- 즉 성경에 기록되어있는 클래스입니다.
두 번째 상황의 경우 코드는 다음과 같습니다.
class MyParentClass {
public static function getInstance() {
return new self();
}
}
class MyChildClass extends MyParentClass {
}
$a = MyChildClass::getInstance();
var_dump($a);
And you'd get this kind of output :
object(MyParentClass)#1 (0) { }
Which means self
means MyParentClass
-- i.e. here too, the class in which it is written.
With PHP < 5.3, that "the class in which it is written" is important -- and can sometimes cause problems.
That's why PHP 5.3 introduces a new usage for the static
keyword : it can now be used exactly where we used self
in those examples :
class MyParentClass {
public static function getInstance() {
return new static();
}
}
class MyChildClass extends MyParentClass {
}
$a = MyChildClass::getInstance();
var_dump($a);
But, with static
instead of self
, you'll now get :
object(MyChildClass)#1 (0) { }
Which means that static
sort of points to the class that is used (we used MyChildClass::getInstance()
), and not the one in which it is written.
Of course, the behavior of self
has not been changed, to not break existing applications -- PHP 5.3 just added a new behavior, recycling the static
keyword.
And, speaking about PHP 5.3, you might want to take a look at the Late Static Bindings page of the PHP manual.
This seems to be an implementation of the Singleton pattern. The function is called statically and checks whether the static class has the variable $_instance
set.
If it isn't, it initializes an instance of itself (new self()
) and stores it in $_instance
.
If you call className::getInstance()
you will get one and the same class instance on every call, which is the point of the singleton pattern.
I've never seen it this done this way, though, and honestly didn't know it was possible. What is $_instance
declared as in the class?
This is most likely used in singleton design pattern, wherein the constructor is defined as private so as to avoid being instantiated, the double colon (::)
operator can access members that are declared static inside the class, so if there are static members, the pseudo variable $this cannot be used, hence the code used self instead, Singletons are good programming practices that will only allow 1 instance of an object like database connector handlers. From client code, accessing that instance would be done by creating a single access point, in this case he named it getInstance()
, The getInstance in itself was the function that created the the object basically using the new keyword to create an object meaning the constructor method was also called.
the line if(!isset(self::instance))
checks if an object has already been created, you could not understand this becuase the code is just a fragment, somewhere in the top, there should be static members like probably
private static $_instance = NULL;
in normal classes we would have accessed this member by simply
$this->_instance = 'something';
but its declared static and so we could not use the $this code we use instead
self::$_instance
by checking if there is an object stored on this static class variable, the class can then decide to create or not to create a single instance, so if its not set, !isset, meaning no object exists on the static member $_instance, then it generates a new object, stored it in the static member $_instance
by the command
self::$_instance = new self();
and returned it to client code. The client code can then happily use the single instance of the object with its public methods, but in the client code, calling the single access point, that is, the getInstance()
method is also tricky, it has to be called like this
$thisObject = className::getInstance();
the reason, the function in itself is declared static.
Yes, it's like new className()
(referring to the class containing that method), probably used in a Singleton pattern where the constructor is private.
If the class is inherited then calling getInstance() from child will not give you a instance of child. It will only returns an instance of parent instance. This is because we call new self().
If you want that the child class will return an instance of child class then use new static() in the getInstance() and it will then return the child class instance. This is called late binding!!
참고URL : https://stackoverflow.com/questions/2396415/what-does-new-self-mean-in-php
'IT박스' 카테고리의 다른 글
Rails-data- * 속성이있는 link_to helper [duplicate] (0) | 2020.08.13 |
---|---|
일치시킬 정규식 패턴, 다음 경우 제외… / 다음 사이 제외 (0) | 2020.08.13 |
번호에서 유니 코드 문자 만들기 (0) | 2020.08.13 |
Doctrine을 사용하여 여러 열로 정렬 (0) | 2020.08.13 |
템플릿의 Angular 2 해시 태그는 무엇을 의미합니까? (0) | 2020.08.13 |