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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型