I try to create app with structure as below:
App
+--src
Example.php
+--vendor
--
composer.json
composer.lock
index.php
Where Example.php set namespace Example
then use it inside index.php
. but it not working.
Example.php
<?php namespace Example
class Example {
public function __construct()
{
echo 'constructed';
}
}
index.php
<?php use Example\Example;
$example = new Example;
echo 'here ok';
but when I runing it in browser, its error:
This page isn't workinglocalhost is currently unable to handle this request.
What am I wrong, any help?
Since you haven't posted what your composer.json
contains yet, at least you must have it in your autoload
:
{
"autoload": {
"psr-4": {
"Example\\": "src"
}
}
}
In your index.php
, you need to require the autoload from composer:
require_once 'vendor/autoload.php'; // add the autoloader
use Example\Example;
$example = new Example;
echo 'here ok';
Lastly, you need to run composer to reflect your changes:
php composer dump-autoload
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.