0

Im trying to wrap my head around some basic MVC OOP coding concepts.

Consider the following.

private $notfound
public function __construct(){
        $this->notFound = function($url){
            echo "404 - $url was not found!";
        };
    }
  • $this->notfound = function($url) So the way Ive been taught is that a function should start with the reserved keyword function name(params) In the statment $this->notfound = function($url)obviously there is no name after function...Im pretty sure I get the gist of the code but for readability sake would like to understand it a bit deeper. Why use a function after $this->notFound...? Why cant it be something like $this->notFound = $var[$url]

  • Usually the closing parenthesis of If, Functions etc does not require a semicolon ; Why do I get an error when I omit the ; for the closing parenthesis for the statment $this->notFound = function{};`

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97
  • 3
    It's not an OOP concept, but an [anonymous function](http://php.net/manual/en/functions.anonymous.php), a function defined inline that can be assigned to a variable to call later – Mark Baker Feb 14 '18 at 08:15
  • 2
    It's an anonymous function which is then assigned to a variable. I think this code was written by a JavaScript developer. – GolezTrol Feb 14 '18 at 08:15
  • Please check the linked question and report back if that's not your doubt (I'm afraid I can't suggest a duplicate link without automatically closing the question). – Álvaro González Feb 14 '18 at 08:16
  • All good explained it nicely – Timothy Coetzee Feb 14 '18 at 08:18
  • 1
    As for the trailing semi-colon, it's because the code preceding the block `function() {...}` is `$this->notfound = ...` which is a variable assignment operation. The code block `function() {...}` is the value, and due to it being a variable assignment it requires a semi-colon to mark the end of the statement. – e_i_pi Feb 14 '18 at 08:22

0 Answers0