Denes Csesznegi
Denes Csesznegi

Reputation: 25

Can't extend abstract PHP class

I want a base class to be extended, but I have some errors coming out:

Fatal error: Class 'Api\Services\Base' not found in /var/www/html/Api/Services/Example.php on line 7

I searched for typos, tried to use the fully qualified name, made the abstract class empty or just defined it as a simple class; none of these helped. Using "require" instead of "use" worked, but still...

Any idea (the two files are in the same directory: /var/www/html/Api/Services)?

Thanks in advance...

<?php
    // Base.php
    namespace Api\Services;

    use Api\Classes\ErrorHandler;
    use Api\Classes\ErrorMessage;

    abstract class Base
    {
        public $data = null;

        public function getData()
        {
            return $this->data;
        }

        public function setData($data = null)
        {
            $this->data = $data;
        }
    }
?>

<?php
    // Example.php
    namespace Api\Services;

    use Api\Services\Base;

    class Example extends Base
    {
        public $request = array();

        public function __construct($request = array())
        {
            $this->request = $request;
        }
    }
?>

Upvotes: 0

Views: 1533

Answers (1)

Dragos
Dragos

Reputation: 1914

use Base

instead of

 use Api\Services\Base;

because you are already inside the namespace Api\Services

Actually, you don't even have to write the use statement, you are inside the namespace, you can just call the classes inside the same namespace without including them (use)

Upvotes: 2

Related Questions