Sammy
Sammy

Reputation: 75

Do I need to define functions before I use them in object-oriented PHP?

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?

Upvotes: 2

Views: 148

Answers (5)

Will
Will

Reputation: 1622

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)

Upvotes: 1

deceze
deceze

Reputation: 522510

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.

Upvotes: 0

OverZealous
OverZealous

Reputation: 39580

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

Upvotes: 4

rlperez
rlperez

Reputation: 1328

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.

Upvotes: 0

karevn
karevn

Reputation: 575

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.

Upvotes: 3

Related Questions