dongshandun4363 2011-07-14 04:33
浏览 55
已采纳

如何找到调用非静态方法的地方?

In the case of the following example, I can not easily find the places where get() of Book class is called by searching files with get.
Because there are a lot of get which do not belong to Book class.

class Book {
  function get() {
  }
}

class Pen {
  function get() {
  }
}

$obj = new Pen;
$obj->get();

$obj = new Book;
$obj->get();

But if I use just functions or static methods like the following codes, I can easily find the places by searching files with Book_get or Book::get.
And if I use functions, I can find both where the function is called and where the function is defined by searching only with Book_get.

function Book_get() {
}

function Pen_get() {
}

Book_get();
Pen_get();


class Book {
  static function get() {
  }
}

class Pen {
  static function get() {
  }
}

Book::get();
Pen::get();

When I use non-static methods, how can I easily find the places where they are called?
Or is it better to use functions or static methods instead of non-static methods as possible?

  • 写回答

4条回答 默认 最新

  • doutan8775 2011-07-14 04:39
    关注

    When I use non-static methods, how can I easily find the places where they are called?

    A class member function can be called in a number of ways,
    Through instance of class
    Through polymorphic behavior(dynamic dispatch)

    For first case, any reasonably good code analysis tool or IDE will be able to tell you the class from which the function is being called depending on type of the object, but I guess there is no easy way to track the second case without examining the code, I doubt any of the code analysis tools are intelligent enough to tell you exactly which class method is being called(Source Insight surely doesn't) because the dispatch is dynamic not compile time.

    Is it better to use functions or static methods instead of non-static methods as possible?

    To use or not use a static method needs much deeper thought than just to be able to find functions in a search.
    Static functions do not have this pointer so you cannot access non static members inside it.They can only operate on static members of the class.

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

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题