猿问

__set() 和 __get() 不能与 isset() 和empty() 一起使用。怎么解决?

具有:


class foo {

    

    private $myproperty;

    

    public

    function __construct() {

        if (

            ( !isset( $_SESSION ) ) &&

            ( session_status() == PHP_SESSION_NONE )

        ) {

            session_start();

        }

    

    public

    function __set( $name, $value ) {

        if ( property_exists( "foo", $name ) ) {

            $_SESSION[ $name ] = $value;

        }

    }

    

    public

    function __get( $name ) {

        if ( property_exists( "foo", $name ) ) {

            return $_SESSION[ $name ];

        }

    }

    

}

正在做:


$foo = new $foo();

$foo->myproperty = "value";

if ( isset ($foo->myproperty ) ) {

  echo "myproperty setted";

} else {

  echo "myproperty not setted"

}

为什么要返回"myproperty not setted"?如果我替换为同样的isset问题 empty。是一个错误吗?如果没有,我该如何修复它?


阿晨1998
浏览 90回答 2
2回答

温温酱

您应该使用 __isset() 方法。这是一个例子:<?phpclass Foo {&nbsp; &nbsp; private $myproperty;&nbsp; &nbsp; public function __construct() {&nbsp; &nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( !isset( $_SESSION ) ) &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( session_status() == PHP_SESSION_NONE )&nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public function __set( $name, $value ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( property_exists( "foo", $name ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION[ $name ] = $value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public function __get( $name ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( property_exists( "foo", $name ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $_SESSION[ $name ];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public function __isset($propertyName){&nbsp; &nbsp; &nbsp; &nbsp; return isset($_SESSION[$propertyName]);&nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$foo = new foo();$foo->myproperty = "value";if ( isset($foo->myproperty) ) {&nbsp; echo "myproperty setted";} else {&nbsp; echo "myproperty not setted";}?>请注意,这种方式empty()也可以工作,因为它在内部使用__isset()。

慕尼黑的夜晚无繁华

isset当您在类级别上已经有了神奇的__GET方法并且您已经在实现相同的功能时,您实际上不需要检查。代码中的第二个拼写错误是类名不是,new $foo();而应该是new foo(). 但是,我建议类名使用标题大小写,例如class Foo.class Foo {private $myproperty;public function __construct() {    if (        ( !isset( $_SESSION ) ) &&        ( session_status() == PHP_SESSION_NONE )    ) {        session_start();    }}public function __set( $name, $value ) {    if ( property_exists( "foo", $name ) ) {        $_SESSION[ $name ] = $value;    }}  public function __get( $name ) {        if ( property_exists( "foo", $name ) ) {            return $_SESSION[ $name ];        }    }   }$foo = new Foo();$foo->myproperty = "value";if ( $foo->myproperty  ) {  echo "myproperty setted";} else {  echo "myproperty not setted";}不过,如果您仍在寻找如何使用isset,您可以将以下方法添加到您的classpublic function __isset($name){    return isset($_SESSION[$name]);}
随时随地看视频慕课网APP
我要回答