开发者

How to find places where a non-static method is called?

开发者 https://www.devze.com 2023-03-20 08:51 出处:网络
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.

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?


You can always temporarily rename get to something like xget and rebuild to find the calling points.


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.


Switching to a better IDE is the simplest solution. Eclipse CDT for example allows you to view the caller hierarchy and find all call sites of a method. (Although, advanced cases of templates and inheritance might still make it hard to figure)


If your method needs access to data in this->, then you have to make it a non-static method. Then the object needs to be instantiated (via new).

If all your method uses is data contained in function parameters, then consider making your method static. Or create a separate class with static methods only.

Book::get();
Pen::get(); this will be the best to use in nonstatic
0

精彩评论

暂无评论...
验证码 换一张
取 消