php小白有关问题

WBOY
Lepaskan: 2016-06-13 13:28:53
asal
1135 orang telah melayarinya

php小白问题

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php class Enum
{
    protected $self=array();
    public function _construct()
    {
        $args=func_get_args();
        for($i=0,$n=count($args);$i<$n;$i++)
            $this->add($args[$i]);
    }
    public function _get($name=null)
    {
        return $this->self[$name];
    }
    public function add($name=null,$enum=null)
    {
        if(isset($enum)) $this->self[$name]=$enum;
        else $this->sef[$name]=end($this->self)+1;
    }
}

class DefinedEnum extends Enum
{
    public function _construct($itms)
    {
        foreach($itms as $name=>$enum)
        $this->add($name,$enum);
    }
}

class FlagsEnum extends Enum
{
    public function _construct()
    {
        $args=func_get_args();
        for($i=0,$n=count($args),$f=0x1;$iadd($args[i],$f);
    }
}

$eFruits=new Enum("APPLE","ORANGE","PEACH");
echo $eFruits->APPLE.",";
echo $eFruits->ORANGE.",";
echo $eFruits->PEACH."\n";

$eBeers=new DefinedEnum("GUINESS"=>25,"MIRROR_POND"=>49);
echo $eBeers->GUINESS.",";
echo $eBeers->MIRROR_POND."\n";

$eFlags=new FlagsEnum("HAS_ADMIN","HAS_SUPER","HAS_POWER","HAS_GUEST");
echo $eFlags->HAS_ADMIN.",";
echo $eFlags->HAS_SUPER.",";
echo $eFlags->HAS_POWER.",";
echo $eFlags->HAS_GUEST."\n";
?>
Salin selepas log masuk


这段代码我怎么看不懂啊
哪里来的$eFruits->APPLE
 $eFruits->ORANGE
$eFruits->PEACH
不要见笑啊




------解决方案--------------------
1、你先把程序修改正确了:方法名前有下划线的方法名一律改成两个下划线的
如 _construct 改为 __construct

2、修改 __get 方法成这样
public function __get($name=null)
{
echo "读取属性 $name";
return $this->self[$name];
}
3、运行修改后的代码,你就可以知道是怎么回事了

------解决方案--------------------
PHP code
 class Enum
{
    protected $self=array();
    public function __construct()
    {
        $args=func_get_args();
        for($i=0,$n=count($args);$iadd($args[$i]);
    }
    public function __get($name=null)
    {
        return $this->self[$name];
    }
    public function add($name=null,$enum=null)
    {
        if(isset($enum)){
            $this->self[$name]=$enum;
        }else{ 
                  $this->self[$name]=end($this->self)+1;
        }        
    }
}

class DefinedEnum extends Enum
{
    public function __construct($itms)
    {
        foreach($itms as $name=>$enum)
        $this->add($name,$enum);
    }
}

class FlagsEnum extends Enum
{
    public function __construct()
    {
        $args=func_get_args();
        for($i=0,$n=count($args),$f=0x1;$iadd($args[$i],$f);
        }
    }
}

 $eFruits=new Enum("APPLE","ORANGE","PEACH");
echo $eFruits->APPLE.",";
echo $eFruits->ORANGE.",";
echo $eFruits->PEACH."\n";

$eBeers=new DefinedEnum(array("GUINESS"=>25,"MIRROR_POND"=>49));
echo $eBeers->GUINESS.",";
echo $eBeers->MIRROR_POND."\n"; 

$eFlags=new FlagsEnum("HAS_ADMIN","HAS_SUPER","HAS_POWER","HAS_GUEST");
echo ($eFlags->HAS_ADMIN).",";
echo ($eFlags->HAS_SUPER).",";
echo $eFlags->HAS_POWER.",";
echo $eFlags->HAS_GUEST."\n";
<br><font color="#e78608">------解决方案--------------------</font><br>两个下划线就都对了。 <div class="clear">
                 
              
              
        
            </div>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan