-1

Good day,

I'm unable to use the libphonenumber framework by using autoloader.php with spl_autoload_register. I'm not using composer, but want to use a PSR4 autoloader.

I have tried a number of examples found here and elsewhere, but no success.

For example: My script path: /opt/lampp/htdocs/project/incl/register.php

Framework path: /opt/lampp/htdocs/project/incl/libphonenumber

in autoloader.php

<?php
spl_autoload_register(function($class) {
  $prefix = 'libphonenumber'; // namespace prefix
  $base_dir = __DIR__.DIRECTORY_SEPARATOR.$prefix.DIRECTORY_SEPARATOR; // base directory for the namespace prefix

echo 'base-dir: ', var_dump($base_dir), '<br>';
echo 'class: ', var_dump($class), '<br>';

  // does the class use the namespace prefix?
  $len = strlen($prefix);

echo 'strncmp: ', var_dump(strncmp($prefix, $class, strlen($prefix))), '<br>';
echo 'relative class: ', var_dump(substr($class, strlen($prefix))), '<br>';

  if (strncmp($prefix, $class, $len) !== 0) {  // using this on "PhoneNumberUtil" doesn't make sense
    return; // no, move to the next registered autoloader
  }

  // get the relative class name
  $relative_class = substr($class, $len);  // using this on "PhoneNumberUtil" doesn't make sense

  // replace the namespace prefix with the base directory, replace namespace separators with directory separators in the relative class name, append with .php
  // $file = $base_dir.str_replace('\\', '/', $relative_class).'.php';
echo 'file with relative-class: ', var_dump($base_dir.str_replace('\\', '/', $relative_class).'.php'), '<br>';
  $file = $base_dir.str_replace('\\', '/', $class).'.php';
echo 'file: ', var_dump($file), '<br>';

  // if the file exists, require it
  if (file_exists($file)) { require_once $file; }
});

Result:

base-dir: string(46) "/opt/lampp/htdocs/project/incl/libphonenumber/"

class: string(15) "PhoneNumberUtil"

strncmp: int(1)

relative class: string(1) "l"

file with relative-class: string(51) "/opt/lampp/htdocs/project/incl/libphonenumber/l.php"

file: string(65) "/opt/lampp/htdocs/project/incl/libphonenumber/PhoneNumberUtil.php"

PhoneNumberUtil.php gets loaded by last line.

Code in register.php:

include_once 'autoloader.php';

$phone_util = PhoneNumberUtil::getInstance();

Result:

Fatal error: Class 'PhoneNumberUtil' not found in /opt/lampp/htdocs/project/incl/register.php

Tried with another autoloader.php:

<?php
spl_autoload_register(function($className) {
  include __DIR__.'/libphonenumber/'.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
});

Same result:

Fatal error: Class 'PhoneNumberUtil' not found in /opt/lampp/htdocs/project/incl/register.php

Can someone please tell me what I'm doing wrong?

EDIT

Some more information:

I want to use the libphonenumber package from https://github.com/giggsey/libphonenumber-for-php

I have tried to follow the example for the autoloader.php as shown there, but with no succes.

Following the examples I found, my code should work, but it doesn't.

I have tried to find some more explanation, but didn't help.

Unfortunately I still cannot understand why my code is not working.

Thank you in advance.

marlon1215
  • 11
  • 4

1 Answers1

-1

Never mind.

I found the solution myself, by trial and error.

By changing this:

$phone_util = PhoneNumberUtil::getInstance();

in register.php to this:

$phone_util = libphonenumber\PhoneNumberUtil::getInstance();

it worked fine.

However, I cannot logically explain why that extra code is needed.

To me it looks like a bug in PHP.

In my opinion it should be taken care of in autoloader.php.

marlon1215
  • 11
  • 4