douxi2011 2011-01-11 12:54
浏览 18
已采纳

空白函数如何在PHP中帮助我们

hello
I'm learning php and i have seen many empty Function in many other small projects and I don't know how it can help.

Example:

public function first(){
// do nothing
          }
public function second(){
        // do nothing
          }
public function myfuntion(){
        // Codes
          }

another example:
class user_bo{

protected $attributes = Array(); 

  public function __get($key){  

      return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;   
  } 

  public function __set($key, $value){  

      $this->attributes[$key] = $value;   
  } 

  /** Constructor **/
  public function __construct(){ }  

} ?>

how can __construct() help us in this situation since it look like it doesn't do anything?

  • 写回答

4条回答 默认 最新

  • dqt83336 2011-01-11 13:03
    关注

    When you say Empty functions, more than lily your talking about __construct and magic methods

    The way they help us to determine authorization of an action, for example

    public class MyClass
    {
        private function __construct(){}
    }
    

    This allows us to block the new MyClass call, but can be overriden by calling a static method and doing new self()

    There are also other reasons such as disallowed inheritance, like so:

    #Class 1
    public function Base
    {
        public function start()
        {
            echo "hello";
        }
    }
    
    #Class 2
    public function Layer extends Base
    {
    }
    
    #Class 3
    public function Layer extends Base
    {
        private function start(){}
    }
    

    looking at the above class 2 contains a callable method called start, but its the method from the parent, in class 3 the same occurs but its overridden by the local method as such, and as its private it disallows the call of the method.

    getting a little deeper into OOP we have interfaces which tell a class it must contain a designated set of methods, so if a class implements such interface it will be required to define all the methods needed, otherwise it will throw an error, so programmers may create empty methods to keep the class structure.


    In light of your new example, There is no specific reason for an empty public __constructor apart from when its extending another class.

    Here's an example:

    class Main
    {
        public function __construct()
        {
            echo "Main";
        }
    }
    
    class FirstSub extends Main
    {
    }
    
    class SecondSub extends Main
    {
        public function __construct()
        {
            echo "SecondSub";
        }
    }
    
    class ThirdSub extends Main
    {
        public function __construct()
        {
            echo "ThirdSub <br />";
            parent::__construct();
        }
    }
    

    As you can see from the structure we have one primary class called Main and three sub class.

    If we initialize the main class were going to get the constructor print Main, but the results will vary with the sub classes as the constructor overrides the parent.

    so initializeing the following will result in:

    • Main: Main
    • FirstSub: `` - Nothing
    • SecondSub:SecondSub
    • ThirdSub: ThirdSub<br />Main

    as you can so that using a blank construct has effect on parent classes.

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分