Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of forward declarations (of functions) in PHP?

Tags:

php

I'm posting this because it wasn't readily apparent on Google or php.net

==> Please demonstrate several examples of the syntax for forward declaring functions, in the PHP language.

For example (in C++):

int foo(); // forward declaration

int x = foo(); // calling the function.

int foo()  // the actual implementation
{
   return 5;
}

How would this look in PHP? How about with multiple arguments? How about with default arguments? What are the best practices and why?

like image 963
Fellow Traveler Avatar asked Aug 30 '12 04:08

Fellow Traveler


People also ask

What is forward declaration of a function?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

What is forward declaration why it is required explain with example?

In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };

How do you perform a forward declaration of a subroutine performed?

To declare a subroutine, use one of these forms: sub NAME ; # A "forward" declaration. sub NAME ( PROTO ); # Ditto, but with prototype.

When can you forward declare?

You will usually want to use forward declaration in a classes header file when you want to use the other type (class) as a member of the class. You can not use the forward-declared classes methods in the header file because C++ does not know the definition of that class at that point yet.


2 Answers

You don't need to do forward declaration in PHP, instead you need to have the function declared, in current script (even if it's after the invocation), or in any included/required script, givent that the include/require statement is executed before you call the function.

There's no forward declaration.

If on the current script, even at the end, after the invocation: it' ok

If it's on a script INCLUDEd/REQUIREd by the current script, the INCLUDE/REQUIRE statement should have been executed BEFORE invocation of the function.

If it's on a script that INCLUDE/REQUIRE the current script: it's ok (even if declared AFTER the INCLUDE/REQUIRE statement)

like image 192
Parallelis Avatar answered Nov 14 '22 22:11

Parallelis


I'd say the requested example is not the best for demonstrating the need of forward declarations. Say, we need the following instead:

function foo($n) {
  echo 'Foo';
  if($n>0) bar($n-1);
}
function bar($n) {
  echo 'Bar';
  if($n>0) foo($n-1);
}
foo(200);

The code above works perfectly in PHP despite of "undefined" function bar inside foo. It appears that PHP checks function definition when it is called.

So the answer is:

  1. PHP doesn't have forward declarations.
  2. The function doesn't have to be defined before it appears in the text, but it must be defined before it is called.
like image 40
Axel Avatar answered Nov 14 '22 21:11

Axel