在面向对象的 PHP 中使用函数之前是否需要定义函数?
不确定我是否正确地表达了这个问题,但让我解释一下。在程序化程序中,例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
函数的顺序并不重要,原因如下:
第三种情况的示例是:
它将调用对象
$object
上的方法my_method
(如果存在),该对象是MyObject< 的实例。 /代码> 类。 (如果该方法不存在,它将抛出运行时异常。)
The order of the functions don't matter, for several reasons:
An example of the third case is:
That will call the method
my_method
, if it exists, on the object$object
, which is an instance of theMyObject
class. (If that method doesn't exist, it will throw a runtime exception.)从技术角度来看——这根本不重要。重要的是可读性和易于修改,因此由您决定什么最适合您。您可能希望按时间顺序、字母顺序或有时按目的对函数进行排序。恕我直言,最好的方法是按字母顺序排列它们 - 这样可以更轻松地在代码导航工具中搜索它们。
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.
取决于语言。
例如,在 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)
即使在过程风格中,函数也不需要按顺序声明。它们只需要在被调用时存在。这将工作得很好,因为这两个函数实际上都没有被调用:
对于 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:
The same goes for OOP, a function must exist when it is called. In which order they are declared is irrelevant.
您可以按任何顺序逻辑地调用它们。某些语言强制执行可能不同的声明顺序。但从根本上来说,如果您使用的模块/类/代码块声明了许多函数或方法,您可能会随意调用它们。
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.