Parse error: parse error awaiting `T_FUNCTION 'in my MVC structure

I follow Create your first Tiny MVC Boilerplate using PHP , and as far as I can tell - my code is identical to Jeff's code ... but I get this error:

Parsing error: while waiting for `T_FUNCTION 'in D: \ WAMP \ WWW \ MVC_test \ application \ load.php on line 8

load.php

<?php

    class Load {
        function view( $file_name, $data = NULL )
        {
            if( is_array($data) ) { extract($data); }
        }
        include 'views/' . $file_name;
    }

?>

I tried several different things, but I don’t understand what is wrong on line 8.

+3
source share
4 answers

This line

include 'views/' . $file_name;

is inside the class, but outside the method, which is impossible in PHP.

+14
source

$ file_name - a local variable inside a function cannot be used outside of it

//replace
       function view( $file_name, $data = NULL )
        {
            if( is_array($data) ) { extract($data); }
        }
        include 'views/' . $file_name;
//with
       function view( $file_name, $data = NULL )
        {
            if( is_array($data) ) { extract($data); }
            include 'views/' . $file_name;
        }
+4

You cannot include in a class definition with an expression

<?php

        class Load {

            function view( $file_name, $data = NULL )
            {
                include 'view/'.$file_name;
                if( is_array($data) ) { extract($data); }
            }

        }

    ?>
+3
source

This line should be

include ('views/' . $file_name);

php include

0
source

Source: https://habr.com/ru/post/1795976/


All Articles