라이브러리

[PHP] ReflectionProperty::isPromoted - 속성이 승격되었는지 확인합니다.




ReflectionProperty::isPromoted


PHP 7.4에서 추가된 `ReflectionProperty::isPromoted` 메소드는 프로퍼티가 promotable 인지 여부를 확인합니다. Promotable 프로퍼티는 PHP 7.4에서 추가된 기능으로, 프로퍼티가 자동으로 상위 타입으로 업그레이드되도록 허용합니다.

예제


#hostingforum.kr
php

class Animal {

    public $name;



    public function __construct($name) {

        $this->name = $name;

    }

}



class Dog extends Animal {

    public $age;



    public function __construct($name, $age) {

        parent::__construct($name);

        $this->age = $age;

    }

}



$reflectionClass = new ReflectionClass('Dog');

$reflectionProperty = $reflectionClass->getProperty('name');



echo $reflectionProperty->isPromoted() ? 'true' : 'false'; // true



$reflectionProperty = $reflectionClass->getProperty('age');

echo $reflectionProperty->isPromoted() ? 'true' : 'false'; // false



위 예제에서 `Animal` 클래스의 `name` 프로퍼티는 promotable 인데, `Dog` 클래스의 `age` 프로퍼티는 promotable 이 아닙니다.

Promotable 프로퍼티란?


Promotable 프로퍼티는 PHP 7.4에서 추가된 기능으로, 프로퍼티가 자동으로 상위 타입으로 업그레이드되도록 허용합니다. 예를 들어, `Dog` 클래스의 `age` 프로퍼티는 `int` 타입이지만, 상위 타입인 `integer` 타입으로 업그레이드될 수 있습니다.

예제


#hostingforum.kr
php

class Dog {

    public $age;



    public function __construct($age) {

        $this->age = $age;

    }

}



$dog = new Dog(10);



var_dump($dog->age); // int(10)



class Dog {

    public $age;



    public function __construct($age) {

        $this->age = $age;

    }

}



$dog = new Dog(10.5);



var_dump($dog->age); // float(10.5)



위 예제에서 `Dog` 클래스의 `age` 프로퍼티는 `int` 타입이지만, 상위 타입인 `integer` 타입으로 업그레이드될 수 있습니다. 예를 들어, `10`은 `int` 타입이지만, 상위 타입인 `integer` 타입으로 업그레이드될 수 있습니다. 반면 `10.5`은 `float` 타입이지만, 상위 타입인 `integer` 타입으로 업그레이드되지 않습니다.

결론


`ReflectionProperty::isPromoted` 메소드는 프로퍼티가 promotable 인지 여부를 확인합니다. Promotable 프로퍼티는 PHP 7.4에서 추가된 기능으로, 프로퍼티가 자동으로 상위 타입으로 업그레이드되도록 허용합니다. 예제를 통해 promotable 프로퍼티의 사용법을 이해할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색