0

I'v a singular proble... if I include files in this manner:

<?php     
    session_start();
    session_regenerate_id();
    foreach (glob("../class/*.php") as $filename)
    {
        echo "$filename\n";
        include $filename;
    }
    foreach (glob("../utilities/*.php") as $filename) {
        echo "$filename\n";
        include $filename;
    }

class ECommerce {
    private $checker;
    private $errorManager;

    /**
     * ECommerce constructor.
     */
    function __construct() {
        $this->checker = new Checker();
        $this->errorManager = new ErrorManager();
    }

the website doesn't work and when I do

$ecommerce = new ECommerce();

it says that

Class 'ErrorManager' not found in path

I thought anything, when I'v thought to try this:

 <?php     
    session_start();
    session_regenerate_id();
    include "../class/Checker.php";
    include "../class/User.php";
    include "../utilities/ErrorManager.php";

class ECommerce {
    private $checker;
    private $errorManager;

    /**
     * ECommerce constructor.
     */
    function __construct() {
        $this->checker = new Checker();
        $this->errorManager = new ErrorManager();
    }

in this manner, it works and anything does what it has to do!
Inside me, the question "why", has made roots and I can't sleep (really).
Why first manner works for all classes except for ErrorManager?
Thank you before!

Here, there is ErrorManager class code (it has the blank construct like Checker class)

<?php
class ErrorManager
{
function __construct() {
}

function getErrorUserNameNotValid() {
    return "Nome inserito non valido";
}

function getErrorUserSurnameNotValid() {
    return "Cognome inserito non valido";
}

function getErrorUserEmailAlreadyExists() {
    return "Email inserita non valida!";
}

function getErrorUserEmailNotValid() {
    return "Email inserita non valida!";
}

function getErrorUserPasswordNotValid() {
    return "Password inserita non valida! Inserisci una password che sia lunga tra i 6 e i 50 caratteri!";
}

function getErrorUserAddressNotValid() {
    return "Indirizzo inserito non valido!";
}

function getErrorUserPhoneNumberNotValid() {
    return "Numero telefonico inserito non valido! Inserisci solo numeri, senza trattini o caratteri speciali!";
}
}

I include a screenshot of folder too
enter image description here

I'm trying this now, with AutoLoader:
This is in ECommerce class:

spl_autoload_register('MyAutoloader::ClassLoader');
spl_autoload_register('MyAutoloader::LibraryLoader');

And this is in MyAutoloaderClass

class MyAutoloader
{
public static function ClassLoader($className)
{
    $path = "../class/";
    include $path.$className.'.php';
}


public static function LibraryLoader($className)
{

    $path = __DIR__."/";
    include $path.$className.'.php';
}

}

It says that

include(../class/ErrorManager.php): failed to open stream: No such file or directory in

ProtoTyPus
  • 1,272
  • 3
  • 19
  • 40
  • @Brett of course, it's coming! – ProtoTyPus Jun 01 '16 at 01:47
  • ../utilities/ErrorManager.php and are you sure that's the correct path. The utilities folder is in the same folder as the classes folder? – Brett Jun 01 '16 at 01:51
  • @Brett yes! If I include with a foreach loop it doesn't work, with manual path yes >_> – ProtoTyPus Jun 01 '16 at 01:55
  • What does the echo say for the 2nd loop? Does it match that of the manual input? – Matt Jun 01 '16 at 01:57
  • @Brett Nope, it shows only "../utilities/Dispatcher.php" as class, it doesn't show "../utilities/ErrorManager.php"! – ProtoTyPus Jun 01 '16 at 02:01
  • @Brett Done! Thank you for your time, however :) – ProtoTyPus Jun 01 '16 at 02:09
  • This is very strange. I can't think of any reason why it wouldn't be displaying. Is there a permissions issue? Are you on windows? – Brett Jun 01 '16 at 02:28
  • @Brett I'm on windows, but I'm trying it also on a Linux Server that hosts my webspace! – ProtoTyPus Jun 01 '16 at 02:37
  • See the last edit. There might be a dependency in Dispatcher getting loaded before ErrorHandler. I strongly recommend to use a class loader as mentioned in my answer. – Pinke Helga Jun 01 '16 at 03:13
  • I've solved changing "include" in "require_once". If u know why this solve the problem, I would read you happily! Thank you for your time :) – ProtoTyPus Jun 01 '16 at 03:16
  • `require_once` or `include_once` prevent includes of the same file multiple times. `require` throws an error when the file does not exist. The message "Class 'ErrorManager' not found in path" is uncommon on multiple includes. The expected error message is: "Fatal error: Cannot declare class , because the name is already in use in on line #". `require_once` can help in unstructured projects. Auto class loaders should be the way to go. Then `require` satisfies and safes some performance eaten by a class loader on the other hand. – Pinke Helga Jun 01 '16 at 03:50
  • I'v added autoloader and it looks for ErrorManager in Class path instead Utilities. Looks more details in my question! – ProtoTyPus Jun 01 '16 at 11:03
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Jun 01 '16 at 19:20
  • It's up to you to handle class load requests in an autoloader callback. You can even register more than one spl-autoload functions, e.g. each one declared in a file inside classes and utilities folder. If you have difficulties with spl-autoload, I suggest you to create a new question about it (if you can not find applicable answers already handled on stackoverflow). – Pinke Helga Jun 02 '16 at 15:38

1 Answers1

2

include on a relative path will search the file based on the path of the called PHP script, not the included PHP file your are calling include from.

To be able to include even if your ECommerce class is used from different locations, try

include __DIR__ . '/' . $filename;

On older PHP versions you can use dirname(__FILE__) instead of __DIR__.

There might be other dependencies before ErrorManager.php gets loaded, e.g. in Dispatcher.php.

What happens if you test the following?

include "../class/Checker.php";
include "../class/User.php";
include "../utilities/Dispatcher.php";
include "../utilities/ErrorManager.php";

Consider to use a class loading mechanism like spl_autoload_register. It makes the class dependency management much easier.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
  • So why Checker has been included well? – ProtoTyPus Jun 01 '16 at 02:21
  • I'v seen the edit. My problem is in foreach loop, the manual include works! – ProtoTyPus Jun 01 '16 at 02:29
  • I've first finished the edit to be more general. If other classes have been successfully included before, then there might be a dependency in other classes earlier, before ErrorManager gets loaded. There could even be an autoload/spl_autoload mechanism. – Pinke Helga Jun 01 '16 at 02:34
  • It still gives me error on ErrorManager class... I'm desperate! – ProtoTyPus Jun 01 '16 at 02:36
  • Open your error log file. What is the full log of one request? – Pinke Helga Jun 01 '16 at 02:39
  • I don't think it could be husefull, however this is it: 2016-06-01 04:44:20,368 [62675159] INFO - plication.impl.ApplicationImpl - ApplicationImpl.externalInstanceListener invocation 2016-06-01 04:44:20,368 [62675159] INFO - ellij.ide.CommandLineProcessor - External command line: 2016-06-01 04:44:20,368 [62675159] INFO - ellij.ide.CommandLineProcessor - Dir: C:\Program Files (x86)\JetBrains\PhpStorm 2016.1.1\jre\jre\bin\. 2016-06-01 04:44:20,368 [62675159] INFO - ellij.ide.CommandLineProcessor - C:\Users\danie\.PhpStorm2016.1\system\log\idea.log – ProtoTyPus Jun 01 '16 at 02:45
  • Looks like an application debug log. Do you run PHP on jetbrain's integrated webserver? Doesn't it provide a separate error.log on web requests? – Pinke Helga Jun 01 '16 at 02:55
  • I'll see your link. However now I've solved all changing "include" in "require_once". If u know why this solve the problem, I would read you happily! Thank you for your time :) – ProtoTyPus Jun 01 '16 at 03:15
  • P.s. As I said in my question, testing manual path like "include ../path" worked! – ProtoTyPus Jun 01 '16 at 03:17
  • P.s. As I mentioned in the answer, I advised manual testing of include of *Dispatcher before ErrorManager*. ;-) – Pinke Helga Jun 01 '16 at 03:54
  • If I include Dispatcher before then ErrorManager it gives me error! But in Dispatcher I don't user ErrorMaanger! – ProtoTyPus Jun 01 '16 at 10:31