Examples of forward declarations (functions) in PHP?

I publish this because it was not easily visible on Google or php.net

==> Please demonstrate some syntax examples. for forward ad in PHP .

For example (in C ++):

int foo(); // forward declaration int x = foo(); // calling the function. int foo() // the actual implementation { return 5; } 

How will it look in PHP? What about a few arguments? What about the default arguments? What are the best methods and why?

+4
source share
6 answers

You do not need to make a forward declaration in PHP, instead you need to declare a function in the current script (even if it is after a call) or in any included / required script, the include / require statement is executed before the function is called.

No forward declaration.

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

If it is on an INCLUDEd / REQUIREd script using the current script, the INCLUDE / REQUIRE statement should have been executed BEFORE the function call.

If it is on a script, INCLUDE / REQUIRE the current script: it is normal (even if declared AFTER the INCLUDE / REQUIRE statement)

+8
source

Would it be ...

 $x = foo(); function foo() { return 5; } echo $x; //echos "5" 

?

+2
source

PHP does not perform forward function declarations (or "prototyping functions"). The closest we have are Object Interfaces and Abstract Methods . They are called "method prototypes" or "method declarations" (a declaration as opposed to a definition that contains c-style semantics)

 Interface MyWidgetInterface { function calculateThings(SomeClass $foo); } Abstract Class MyAbsClass { public abstract function sayHello(); } Class MyObj Extends MyAbsClass Implements MyWidgetInterface { public function calculateThings(SomeClass $foo) { } public function sayHello() { echo "Hi!\n"; } } $thing = new MyObj(); $thing->sayHello(); 

One of the reasons we don't have / need to forward declared functions is because php parses your script before compiling it every time. It finds your functions before it starts executing the file, so it knows that they are. In fact, I canโ€™t remember how far back you even needed to configure the function before calling it. I have been doing PHP for 9 years and it never needed it :-)

+2
source

I would say that the requested example is not the best to demonstrate the need for advanced ads. Let's say we need the following:

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

This code works fine in PHP, despite the "undefined" bar function inside foo . It looks like PHP is checking the definition of a function when it is called.

So the answer is:

  • PHP has no forward declarations.
  • A function must not be defined before it appears in the text, but it must be defined before it is called.
+2
source

If you want a function to return a specific type value in PHP, you can use something like this.

 function foo($param) { // code return (int) $param; // or (string), (float) } 

Or when you access the function:

 $x = (int) foo($param); 
0
source

Referring to the default argument, you can do it like this:

 function foo( $param1="value1" , $param2="value2" ){ echo $param1." ".$param2; } foo(); foo("not default","not default"); foo(NULL,"not default"); 
0
source

All Articles