-3

I'm very confusing about namespace on php (>5.4.*) and composer

Scenario

|vendor
   |autoload.php
    |abkrim
       |cputils
          |src
             |CpPanelBackTest.php

Code CpPanelBackTest.php

namespace Abkrim\Cputils;
class CpPanelBackTest
{
    public static function getDate(){
      $year = date('Y');
      $month = date('m');
      $day = date('d');
      return $day . '/' . $month . '/' . $year;
    }
}

File test.php

require __DIR__.'/../../vendor/autoload.php';
use Abkrim\Cputils\CpPanelBackTest;
echo 'Date today is: ' . CpPanelBackTest::getDate();

File composer.json on vendor/abkrim/cputils

"autoload": {
   "psr-4": {
      "Abkrim\\Cputils\\": "src/"
   }
}

Get error ->

PHP Fatal error:  Class 'CpPanelBackTest' not found in at file xxxx line xxxx

If change code for CpPanelBackTest.php

//namespace Abkrim\Cputils; //coment namespace
class CpPanelBackTest
{
    public static function getDate(){
      $year = date('Y');
      $month = date('m');
      $day = date('d');
      return $day . '/' . $month . '/' . $year;
    }
}

And file test.php

//require __DIR__.'/../../vendor/autoload.php';
//use Abkrim\Cputils\CpPanelBackTest;
require_once __DIR__ . '/../../vendor/abkrim/cputils/src/CpPanelBackTest.php';

Now work fine.

I'm lost. I'm not understanding anything about it echo 'Date today is: ' . CpPanelBackTest::getDate();

double-beep
  • 5,031
  • 17
  • 33
  • 41
abkrim
  • 3,512
  • 7
  • 43
  • 69
  • Incredible. 4 negative points and any comment. Any moderator for this bad practices? – abkrim Jun 28 '18 at 07:18
  • 1
    Note that it is considered bad practice to have "SOLVED" written in a title. Stack Overflow encourages solutions to be given in answers of their own, and the most helpful one marked as accepted with a green checkmark. You may answer your own questions and you may accept your own answers. – Cœur Jul 17 '18 at 14:22

1 Answers1

1

The error you are experiencing is triggered when PHP cannot load or find a class. The name in that error message is the fully qualified name of the class being searched.

That name in your error message does not contain a namespace. That means your usage was not being detected as using a namespace. Which is correct if you look at the one line: echo 'Date today is: ' . CpPanelBackTest::getDate(); No namespace here.

You do have a use statement before that, and it is supposed to import the class into the current (global) namespace. And I believe it would do that - my tests show that the code posted in your question would probably correctly work. The problem is: I find it difficult to create a whole test case with Composer, a separate package, including it and so on, so I did only run a tiny replacement test which does NOT take into account the differences of the file system.

<?php

namespace Foo {
class Bar {}
}

namespace {
use Foo\Bar;
var_dump(new Bar);
}

See it in action: http://3v4l.org/mrVsI

And this is my guess: PHP classes are NOT case sensitive, but the conversion from a class name into a path and file name IS case sensitive on some filesystems. If you mistyped only one letter in your code, Composer will be unable to detect the prefix and/or will be unable to locate the correct file.

Unfortunately you didn't do a correct test to prove that the autoloading is wrong. You should have only removed the autoloader, and instead require the file containing the class - without disabling the namespace.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • more simple.. add require 'vendor/autoload.php'; on test.php and work fine. – abkrim Aug 05 '15 at 16:26
  • question it's more complex. I assk for scenario, using composer+php5+psr-4. When run composer if you composer.json package and your composer.json bundle are ok, it must be write on vendor/composer/autoload_psr4.php a line for correct aplication you namespace psr-4 compilant of your bundle. – abkrim Aug 05 '15 at 16:28