Monday, February 27, 2017

what is an application for the return directive in PHP, outside of global scope

I just encountered the use of return in a PHP framework (Laravel 4) which I am new to.

I understand what it does, as follows:

# contents of file A
<?php
//this is fileA.php
echo '<pre>';
echo "here is from file A\n";
$output = require('fileB.php');
echo $output;  // "Hi, I am the return value from file B"
?>

# contents of file B
<?php 
echo "Hi, I am content inside file B\n";
$return = "Hi, I am the return value from file B";
return $return;  //terminate page processing..
//should not show
echo "I bet you don't see me\n";
<?php

the above outputs:

Here is from file A
Hi, I am content inside file B
Hi, I am return value from file B

My question is, when is it advisable (or even on God's green earth thinkable!) to use something like this? This seems analogous to return in a function but essentially makes an included file a "quasi-function" and blurs the lines between object oriented (or even procedural) coding and essentially elevates a file to the level of a method or a class. Again, I'm wondering when such a thing would be advisable OVER an OOP application.

Oh, and did Laravel have any particular idea in mind in implementing this so commonly..




via Oliver Williams

Advertisement