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

发布于 2024-11-23 18:57:50 字数 819 浏览 2 评论 0原文

在以下示例的情况下, 使用get搜索文件,无法轻易找到调用Book类get()的地方。
因为有很多 get 不属于 Book 类

class Book {
  function get() {
  }
}

class Pen {
  function get() {
  }
}

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

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

但是如果我只使用函数或静态方法,如下面的代码, 我可以通过使用 Book_getBook::get 搜索文件来轻松找到这些地点。
如果我使用函数,只需使用 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();

当我使用非静态方法时,如何轻松找到调用它们的地方?
还是尽可能使用函数或静态方法而不是非静态方法更好?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开浅夏 2024-11-30 18:57:51

您始终可以临时将 get 重命名为 xget 之类的名称,然后重新构建以查找调用点。

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

感情旳空白 2024-11-30 18:57:51

当我使用非静态方法时,如何轻松找到调用它们的地方?

类成员函数可以通过多种方式调用,
通过类的实例
通过多态行为(动态调度)

对于第一种情况,任何相当好的代码分析工具或 IDE 将能够根据对象的类型告诉您调用函数的类,但是
我想没有简单的方法可以在不检查代码的情况下跟踪第二种情况,我怀疑任何代码分析工具是否足够智能,可以准确地告诉您正在调用哪个类方法(Source Insight 肯定不会),因为调度是动态的不是编译时的。

尽可能使用函数或静态方法而不是非静态方法更好吗?

使用或不使用静态方法需要更深入的了解不仅仅是能够在搜索中找到功能。
静态函数没有 this 指针,因此您无法访问其中的非静态成员。它们只能对类的静态成员进行操作。

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.

草莓味的萝莉 2024-11-30 18:57:51

切换到更好的 IDE 是最简单的解决方案。例如,Eclipse CDT 允许您查看调用者层次结构并查找方法的所有调用站点。 (尽管模板和继承的高级案例可能仍然让人难以理解)

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)

人生百味 2024-11-30 18:57:51

如果您的方法需要访问 this-> 中的数据,那么您必须将其设为非静态方法。然后需要实例化该对象(通过new)。

如果您的方法使用的所有数据都是函数参数中包含的数据,那么请考虑将您的方法设为静态。或者创建一个仅包含静态方法的单独类。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文