drn1008 2015-07-30 03:23
浏览 13
已采纳

一些PHP问题(echo / OO PHP)[关闭]

I learning a bit of OO PHP but the docs I came across showed a couple of methods of the following examples. I am confused as to why both work, and which should be used exclusively.

I know that #1 is not OOP but I was still curious about the second method of echo, and if it should be used or not.

  1. Both of the echo's below print "Lansana", but one initializes $name before the echo whereas the other initializes it after (or during) the echo.

    <?php
    
    $name = "Lansana";
    echo $name;
    
    echo $name = "Lansana";
    
    ?>
    

  1. Notice how there is a public property $name with no value in the first example, and no public property in the second, yet both still work the same.

    class Pets
    {
        public $name;
    
        public function __construct($name) {
            $this->name = $name;
        }
    }
    
    $dog = new Pets("Buddy");
    echo $dog->name;
    

    class Pets
    {
        public function __construct($name) {
            $this->name = $name;
        }
    }
    
    $dog = new Pets("Buddy");
    echo $dog->name;
    

What is the preferred method in #1 and #2, and why? I don't know why the docs showed the first class method because I don't see the need for a public property there, but then again what do I know.

  • 写回答

3条回答 默认 最新

  • dongwo5589 2015-07-30 03:48
    关注

    Thought I should give a little more context to what I outlined in the comments above.

    For the First example try not to mix assignment and output in the same statement. Although it is syntactically correct it doesn't immediately indicate the intent of the statement.

    For the second example as I stated in the comments explicitly defining properties is always preferable. The advantages are:

    1. Most IDE's will auto-complete defined properties for you minimizing the risk of mistyping a property.
    2. It clearly indicates to someone reading the code what properties are available on the object.
    3. It allows for you to set the visibility of the property so you can control more how your class is used which promotes encapsulation.

    One of the most common bugs that you see with OO PHP is when you silently create a property for example something like:

    class A {
        public function setUserId($id){
            $this->userId = $id;
        }
    
        public function getUserId(){
            return $this->userid;
        }
    }
    

    The intent is clear here but you have to be paying pretty close attention to the fact that the referenced properties are cased differently. If becomes much harder if you throw in another 50 lines of code and the two property references aren't on the screen at the same time.

    On of the uses for PHP's magic __get and __set method are to help eliminate this problem earlier in development. Example:

    class A {
        public $foo;
        public $bar;        
    
        public function __set($prop, $val){
            throw new Exception ($prop." does not exist on this object");
        }
    
        public function __get($prop){
            throw new Exception ($prop." does not exist on this object");
        }
    }
    

    This makes it where if you attempt to access an undefined property the class will throw an exception letting you know exactly what happened and where.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)