Inheritance of Interface that is in another folder

Asked

Viewed 97 times

-1

How can I implode that Interface in my classes?

class/layout/Header.php

class Header implements Interface {
    private function setHeader()
    {

    }

    public function getHeader()
    {

    }
}

class/interface/Interface.php

interface Interface {
    public function getHeader();
}

class/autoload.php

function getClass($class) 
{
    $dir = ['interface', 'layout'];

    foreach ($dir as $row) :
        $files = "class/{$row}/{$class}.php";
        if (file_exists($files)) :
          require_once $files;
        endif;
    endforeach;
}

spl_autoload_register("getClass");

index php.

require_once "class/autoLoad.php";

Parse error: syntax error, Unexpected 'Interface' (T_INTERFACE), expecting Identifier (T_STRING) or namespace (T_NAMESPACE) or (T_NS_SEPARATOR) in C: xampp htdocs class layout Header.php on line 5

1 answer

2


Word Interface is reserved in PHP. You may not use this word as class name, namespaces or the like.

When you receive the message as:

syntax error, unexpected 'Interface' (T_INTERFACE)

Just know what the token indicated between parentheses means, then in case you can read the tokens in:

Following the list, the token T_INTERFACE represeta the Object Interfaces, soon you tried to use the syntax Interface (created by you) and it was not expected by the PHP interpreter, causing the error: syntax error, Unexpected 'Interface'.

To solve just exchange the name Interface for a name that is not a reserved word, something like MainInterface.


Extra:

Do not forget that if the production server is Linux, or Unix-like you will not be able to use includes with names like this include 'src/foo.php'; and files named like this src/Foo.php, because linux and Unix-like are usually case-sensitive.

More details about running PHP on Windows and Linux here: PHP on linux or windows

Browser other questions tagged

You are not signed in. Login or sign up in order to post.