在面向对象的 PHP 中使用函数之前是否需要定义函数?

发布于 2024-11-29 21:55:46 字数 486 浏览 1 评论 0原文

不确定我是否正确地表达了这个问题,但让我解释一下。在程序化程序中,例如:

function getUserId()
{
   // some stuff
   return $someUserId;
}

function getUsername()
{
  $id = getUserId();
  $query = mysql_query(" SELECT * FROM users WHERE id = '$id' ");
  while ($row = mysql_fetch_assoc($query))
  {
     return $row['username'];
  }
}

在上面,getUsername 函数调用了 getUserId 函数。由于该函数被调用,因此它必须位于调用它的函数之上,否则它将无法工作。

我想知道的是,在面向对象编程中,类中函数(方法)的顺序重要吗?即使被调用的方法是在脚本中进一步定义的,我是否可以在另一个方法中调用该方法?

Not sure if I phrased the question correctly but let me explain. In a procedural program, for example:

function getUserId()
{
   // some stuff
   return $someUserId;
}

function getUsername()
{
  $id = getUserId();
  $query = mysql_query(" SELECT * FROM users WHERE id = '$id' ");
  while ($row = mysql_fetch_assoc($query))
  {
     return $row['username'];
  }
}

In the above, the getUsername function called the getUserId function. Since that function was called it must be above the one that called it, otherwise it won't work.

What I would like to know is, in object oriented programming, does the order of functions (methods) in a class matter? Can I call a method within another method even if the method being called is defined further down in the script?

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

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

发布评论

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

评论(5

你的他你的她 2024-12-06 21:55:46

函数的顺序并不重要,原因如下:

  1. 这些方法不会在编译时执行,因此编译器可以“预见”可能尚不存在的函数。 (注意:这过于简单化了...)
  2. PHP 是一种动态语言,因为函数甚至不必在编译时存在。它们可以在运行时动态加载。
  3. PHP 按名称加载方法,因此它会在运行时查找与名称匹配的方法。

第三种情况的示例是:

$object = new MyObject();
$method = 'my_method';
echo $object->${my_method}();

它将调用对象 $object 上的方法 my_method(如果存在),该对象是 MyObject< 的实例。 /代码> 类。 (如果该方法不存在,它将抛出运行时异常。)

The order of the functions don't matter, for several reasons:

  1. The methods are not executed at compile time, so the compiler can "look ahead" for functions that might not exist yet. (Note: this is an oversimplification...)
  2. PHP is a dynamic language, in that functions don't even have to exist at compile time. They can be loaded dynamically during the run time.
  3. PHP loads methods by name, so it will look for a method that matches the name during runtime.

An example of the third case is:

$object = new MyObject();
$method = 'my_method';
echo $object->${my_method}();

That will call the method my_method, if it exists, on the object $object, which is an instance of the MyObject class. (If that method doesn't exist, it will throw a runtime exception.)

度的依靠╰つ 2024-12-06 21:55:46

从技术角度来看——这根本不重要。重要的是可读性和易于修改,因此由您决定什么最适合您。您可能希望按时间顺序、字母顺序或有时按目的对函数进行排序。恕我直言,最好的方法是按字母顺序排列它们 - 这样可以更轻松地在代码导航工具中搜索它们。

From technical standpoint - it does not matter at all. All that matters is readability and easy of modification, so it is up to you to decide what works best for you. You may want to order functions chronologically, alphabetically, or, sometimes, by purpose. IMHO, the best way is to order them alphabetically - it makes it easier to search for them in code navigation tools.

⊕婉儿 2024-12-06 21:55:46

取决于语言。

例如,在 JavaScript 中,可以先定义 getUsername() 函数,只要在定义 getUserId() 之后调用它即可。

在Java中,作为另一个例子,它通常并不重要(静态初始化块除外)

Depends on the language.

In JavaScript for example, your getUsername() function can be defined first, so long as it is called after getUserId() has been defined.

In Java as another example, it generally does not matter (except for static initilization blocks)

魔法唧唧 2024-12-06 21:55:46

即使在过程风格中,函数也不需要按顺序声明。它们只需要在被调用时存在。这将工作得很好,因为这两个函数实际上都没有被调用:

function foo() {
    bar();
}

function bar() {
}

对于 OOP 也是如此,函数在被调用时必须存在。它们的声明顺序无关紧要。

Even in procedural style functions don't need to be declared in order. They only need to exist at the time they are called. This will work perfectly fine, because neither function is actually ever called:

function foo() {
    bar();
}

function bar() {
}

The same goes for OOP, a function must exist when it is called. In which order they are declared is irrelevant.

那小子欠揍 2024-12-06 21:55:46

您可以按任何顺序逻辑地调用它们。某些语言强制执行可能不同的声明顺序。但从根本上来说,如果您使用的模块/类/代码块声明了许多函数或方法,您可能会随意调用它们。

You can call them logically in any order. Some languages enforce an order of declaration which may/maynot be different. But at the root, if you are using a module/class/chunk of code that declares a number of functions or methods you may call them entirely willy nilly.

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