-3

Whenever I try to extend a class I end up with

Fatal error: Class 'home\register\Create' not found in C:\xampp\htdocs\project\home\register\register.php on line 8

I have 2 file classes under the same directory first one an abstract class Create :

<?php

namespace home\register;

use home\libs\Tools\Sanitize\Sanitize as sanitize;

abstract class Create
{
    public $sanitize;

    function __construct ()
    {
        $this->sanitize = new sanitize();
        if (isset($_POST)){
            foreach ($_POST as $key => $value){
                if (!empty($_POST["$key"])){
                    $this->$key = $this->sanitize->clean($value);
                }
            }
        }
    }

    abstract function db_query($pdo_db_name, $password, $query, $host = 'localhost');
}

And a second class register which extends Create:

<?php

namespace home\register;

use PDO as pdo;
use home\libs\MainLogger\MainLogger as logger;

class register extends Create //Line 8 Error is thrown when extending the class

I can post the whole code but I doubt the problem is there. I've been trying to adapt to the structural design pattern as I see it as the tidiest way of handling a lot of classes

  • 3
    The error occurs on line 8 of `register.php`. Could you at least include this line, maybe some more, in your snippet above? – digijay Dec 24 '18 at 21:34
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – yivi Dec 25 '18 at 07:59
  • The error is thrown when extending Create which is the last line above I'll update my question for more clarity – Milko Ivanov Dec 25 '18 at 14:31

1 Answers1

0

Right now

You'll need to ensure that Create is loaded before you can extend it in register.php. The simplest way to do this is using require_once, by adding something like:

require_once(__DIR__ . '/Create.php');

to the top of register.php. This should be sufficient for now.

Maybe later

Once you start to get into loading many classes, you may want to look at using an autoloader. From my experience the most flexible way to do this is to convert the project to using composer, and then using the autoload configuration item.

The layout of your classes (not including ones not shown in this question) would be something like:

src/register/Create.php
src/register/register.php

and, to match this, your composer.json would look something like:

{
    "autoload": {
        "psr-4": {"home\\": "src/"}
    }
}

You can then generate the autoloader by running:

composer dump-autoload

and use it in the entry point to your project by adding:

require_once('vendor/autoload.php');

From this point on, you would not need to add individual require_once lines for each file, provided you re-ran dump-autoload after adding new classes to your source.

msbit
  • 4,152
  • 2
  • 9
  • 22
  • Okay that worked like a charm. Thank you. But I have some questions so even if I `use` a class it's not loaded in the memory? What if I have script which loads them all? Will they stay loaded into the memory ,or do I need to include the file that includes all of them? This probably goes close to what composer is from what I understood – Milko Ivanov Dec 25 '18 at 14:35
  • The way auto loading works, is when a class is not found that was previously loaded (included) then the spl autoload function is called with the fully qualified class name (namespace\class). The autoloader takes that and works out the path to the file, and includes it. – ArtisticPhoenix Dec 25 '18 at 18:02
  • use statment, is like a shortcut to the classname. It doesn't load anything. Once a class is included and parsed by PHP it's there tell the end of the request. In fact that is the main reason for namespaces, so classes named the same don't conflict with each other (as they stay loaded) – ArtisticPhoenix Dec 25 '18 at 18:04