skip to Main Content

I’m learning php and composer following the article

I try to use external dependancy composer require phpunit/php-timer.

Here is my composer.json:

{
  "name": "ypapax/composer_php_hello_world_log4php",
  "minimum-stability": "dev",
  "require": {
    "php": ">= 7.2",
    "phpunit/php-timer": "^2.1@dev"
  },
  "autoload": {
    "psr-0": {
      "HelloWorld": "src/"
    },
    "classname": {
      "PHP_Timer": "src/"
    }
  }
}

and my test.php:

<?php

// Autoload files using Composer autoloader.
require_once __DIR__ . '/../vendor/autoload.php';

use HelloWorldGreetings;

echo Greetings::sayHelloWorld();

Where greetings.php is

<?php

namespace HelloWorld;

use PHP_Timer;

class Greetings
{
    public static function sayHelloWorld()
    {
        $timer = new PHP_Timer();
        $timer . start();
        return 'Hello Worldn' . $timer->resourceUsage() . "n";
    }
}

When I run the test php tests/test.php
it gives me an error:

PHP Fatal error:  Uncaught Error: Class 'PHP_Timer' not found in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php:11
Stack trace:
#0 composer_php_hello_world_log4php/tests/test.php(8): HelloWorldGreetings::sayHelloWorld()
#1 {main}
  thrown in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php on line 11

Fatal error: Uncaught Error: Class 'PHP_Timer' not found in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php:11
Stack trace:
#0 composer_php_hello_world_log4php/tests/test.php(8): HelloWorldGreetings::sayHelloWorld()
#1 {main}
  thrown in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php on line 11

I guess something wrong is in composer.json:

"classname": {
      "PHP_Timer": "src/"
    }

PHP version:

$ php --version
PHP 7.3.9 (cli) (built: Sep 14 2019 18:07:55) ( NTS )

Link to my test repo

Update

Here is my file autoload_namespaces.php:

<?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'HelloWorld' => array($baseDir . '/src'),
);

And autoload_classmap.php:

<?php

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'SebastianBergmann\Timer\Exception' => $vendorDir . '/phpunit/php-timer/src/Exception.php',
    'SebastianBergmann\Timer\RuntimeException' => $vendorDir . '/phpunit/php-timer/src/RuntimeException.php',
    'SebastianBergmann\Timer\Timer' => $vendorDir . '/phpunit/php-timer/src/Timer.php',
);

3

Answers


  1. Chosen as BEST ANSWER

    The use statement was missing from the Greetings class:

    <?php
    
    namespace HelloWorld;
    
    use SebastianBergmannTimerTimer;
    
    class Greetings
    {
        public static function sayHelloWorld()
        {
            $timer = new Timer();
            $timer::start();
            return 'Hello Worldn' . $timer->resourceUsage() . "n";
        }
    }
    

    and this can be removed from composer.json:

        "classname": {
          "PHP_Timer": "src/"
         }
    

  2. You are not loading the right namespace. I suggest you to checkout the examples on the package page

    Login or Signup to reply.
  3. I think you don’t need this in your composer.json:

    "classname": {
      "PHP_Timer": "src/"
    }
    

    According to https://github.com/sebastianbergmann/php-timer/blob/master/src/Timer.php you need

    use SebastianBergmannTimerTimer as PHP_Timer;
    

    in your greetings.php file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search