Php tips-php Tutorial

Source: Internet
Author: User
Php: PHPcode & lt ;? PhpclassEnum {protected $ selfarray (); publicfunction_construct () {$ argsfunc_get_args (); for ($ i0, $ ncount ($ args); $ I & lt; $ n; $ I ++) php FAQ
PHP code
  
  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;$i<$n;$i++,$f*=0x2)        $this->add($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";?>


Why can't I understand this code?
$ EFruits-> APPLE
$ EFruits-> ORANGE
$ EFruits-> PEACH
Don't laugh.




------ Solution --------------------
1. modify the program correctly: All the method names with underscores before the method names are changed to two underscores.
For example, change _ construct to _ construct.

2. modify the _ get method as follows:
Public function _ get ($ name = null)
{
Echo "read attribute $ name ";
Return $ this-> self [$ name];
}
3. run the modified code and you will know what is going on.

------ Solution --------------------
PHP code
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-> 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; $ I <$ n; $ I ++, $ f * = 0x2) {$ this-> add ($ 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, "pai_pond" => 49); echo $ eBeers-> GUINESS. ","; echo $ eBeers-> pai_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 ";
------ Solution --------------------
Both underlines are correct.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.