简体   繁体   中英

Composer Autoload does not autoload class

I am following an tutorial on how to create a composer develop envoirement. https://www.hostinger.com/tutorials/how-to-install-composer

I followed this but am getting the error 'Class "Timer" not found'.

Steps i took:

  1. created a folder for the project
  2. did the command "composer require phpunit/php-timer" on the folder. This automatically created the neccesary composer files.
  3. created an php file called demo.php and copied the code from the tutorial (see code below)
  4. Tried to run the php file with the command "php demo.php" and getting the error.

Code:

<?php
require __DIR__ . '/vendor/autoload.php';
Timer::start();
// your code
$time = Timer::stop();
var_dump($time);
print Timer::secondsToTimeString($time)

The classes in phpunit/php-timer are in a namespace called SebastianBergmann\\Timer . Thus, you must either use the fully qualified class name:

\SebastianBergmann\Timer\Timer::start();

Or specify a use alias:

use SebastianBergmann\Timer\Timer;
Timer::start();
// ...
Time::stop();

I haven't looked in much detail but note that the docs for this package do not show static usage, so you might have to do:

use SebastianBergmann\Timer\Timer;
$timer = new Timer();
$timer->start();
// ...
$timer->stop();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM