라이브러리

[PHP] Closure::__construct - 인스턴스화를 허용하지 않는 생성자




Closure::__construct

PHP 7.0.0부터 Closure 클래스는 `__construct` 메서드를 지원합니다. 이 메서드는 Closure 객체를 생성할 때 호출됩니다. 하지만, Closure 객체는 생성자를 호출할 수 없습니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure::__construct 메서드는 호출할 수 없습니다.

// $func->__construct();



Closure::__invoke

Closure 객체는 `__invoke` 메서드를 호출하여 함수를 호출할 수 있습니다. 이 메서드는 Closure 객체를 함수처럼 호출할 때 호출됩니다.

#hostingforum.kr
php

$func = function($name) {

    echo "Hello, $name!";

};



// Closure 객체를 함수처럼 호출할 수 있습니다.

$func->__invoke("John"); // Hello, John!



Closure::__set

Closure 객체는 `__set` 메서드를 호출하여 속성을 설정할 수 있습니다. 이 메서드는 Closure 객체의 속성을 설정할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체의 속성을 설정할 수 있습니다.

$func->__set("name", "Closure");

echo $func->name; // Closure



Closure::__get

Closure 객체는 `__get` 메서드를 호출하여 속성을 가져올 수 있습니다. 이 메서드는 Closure 객체의 속성을 가져올 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체의 속성을 가져올 수 있습니다.

$func->__set("name", "Closure");

echo $func->name; // Closure



Closure::__isset

Closure 객체는 `__isset` 메서드를 호출하여 속성이 설정되었는지 확인할 수 있습니다. 이 메서드는 Closure 객체의 속성이 설정되었는지 확인할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체의 속성이 설정되었는지 확인할 수 있습니다.

$func->__set("name", "Closure");

var_dump(isset($func->name)); // bool(true)



Closure::__unset

Closure 객체는 `__unset` 메서드를 호출하여 속성을 삭제할 수 있습니다. 이 메서드는 Closure 객체의 속성을 삭제할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체의 속성을 삭제할 수 있습니다.

$func->__set("name", "Closure");

unset($func->name);

var_dump(isset($func->name)); // bool(false)



Closure::__call

Closure 객체는 `__call` 메서드를 호출하여 메서드를 호출할 수 있습니다. 이 메서드는 Closure 객체의 메서드를 호출할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체의 메서드를 호출할 수 있습니다.

$func->__call("sayHello", ["John"]); // Hello, John!



Closure::__callStatic

Closure 객체는 `__callStatic` 메서드를 호출하여 정적 메서드를 호출할 수 있습니다. 이 메서드는 Closure 객체의 정적 메서드를 호출할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체의 정적 메서드를 호출할 수 있습니다.

$func->__callStatic("sayHello", ["John"]); // Hello, John!



Closure::__sleep

Closure 객체는 `__sleep` 메서드를 호출하여 serialize할 수 있는 속성을 가져올 수 있습니다. 이 메서드는 Closure 객체를 serialize할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체를 serialize할 수 있습니다.

$func->__set("name", "Closure");

$serialized = serialize($func);

var_dump(unserialize($serialized)); // Closure 객체



Closure::__wakeup

Closure 객체는 `__wakeup` 메서드를 호출하여 unserialize된 속성을 초기화할 수 있습니다. 이 메서드는 Closure 객체를 unserialize할 때 호출됩니다.

#hostingforum.kr
php

$func = function() {

    echo "Closure 객체";

};



// Closure 객체를 unserialize할 수 있습니다.

$func->__set("name", "Closure");

$serialized = serialize($func);

unserialize($serialized);

var_dump($func->name); // Closure



위의 예제는 Closure 클래스의 메서드를 호출하는 방법을 보여줍니다. 하지만, Closure 객체는 생성자를 호출할 수 없으며, 속성을 직접 설정할 수 없습니다. 대신, Closure 객체의 속성을 설정할 때 `__set` 메서드를 호출할 수 있습니다. 또한, Closure 객체의 속성을 가져올 때 `__get` 메서드를 호출할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 654 페이지

검색

게시물 검색