PHP Fatal error: Interface 'App\\PluginContract' not found - php

In the same direcory I have:
html/app
Plugin.php
PluginContract.php
GetUid.php
On GetUid.php i have this code:
<?php
namespace App;
include_once "Plugin.php";
include_once "PluginContract.php";
use Carbon\Carbon;
use TeamSpeak3\Ts3Exception;
class GetUid extends Plugin implements PluginContract
{
public function isTriggered()
{
...
}
}
And this is PluginContract.php:
<?php
namespace App;
interface PluginContract
{
public function isTriggered();
}
It seems all ok, but I got this error:
PHP Fatal error: Interface 'App\PluginContract' not found in /var/www/html/app/GetUid.php on line 11
The weird thing is that it can load Plugin.php without problem but it gets this error for PluginContract.php which is in the same folder.
What I'm doing wrong?

What I was tring to do is add a web interface to a PHP application, I though that I don't need the autoload because it automatically start with the server, but I discovered that adding again the autoload to my index.php I resolved the problem.

Related

How to use class in another class in this same folder using PHP?

I have a folder test. In this folder I have a two classes, TestHeader and TestHeaders.
This is TestHeader class
<?php
class TestHeader
{
public function send(): void
{
}
}
This is TestHeaders class
<?php
class TestHeaders
{
public function profile(int $userId): TestHeader
{
return new TestHeader();
}
}
And when I run tests, show me error Fatal error: Uncaught Error: Class "TestHeader" not found in C:\project\test\TestHeaders.php:7 I don't know why show this error. Someone could me explain why this error is and how I can fix it ?
if you'r not using a php framework that implement the autoload and if you not set up an autoload strategy you have to include all file you'r trying to use in another file.
add include("TestHeader.php"); at start of TestHeaders class, on the line immediately after the <?php tag

Namespace with extends or interface causes Fatal Error without autoloader

Why doesn't this work?
web/index.php (Not web/src/App/App.php)
<?php
namespace App;
// web/index.php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new App();
class App extends \Silex\Application {
public function __construct()
{
parent::__construct();
echo 'Worked!';
}
}
I also tried namespace App {...}, no change. It throws this exception:
Fatal error: Uncaught Error: Class 'App\App' not found in /path/to/web/index2.php:8
Stack trace:
#0 {main}
thrown in /path/to/web/index2.php on line 8
As long as I remove the extends ... and the parent call part, it works. I also noticed interface does the same thing (trying to use Serializable). Is this an issue with the autoloader being confused? Is there a way to do this without putting the App\App class into a file in src\App\App.php?
Note: this is an exercise to build a single-file application with Silex, so "just put it in a file" isn't an answer. I want to know why this doesn't work, which has an answer.
The problem in your code is,
In namespace, You are initiating class object before it being declared and loaded. In your above code you are doing same thing,
1. You are initiating App class object which lies in App namespace
2. You are initiating class object at the moment class when is not yet declared(As it is defined below in your code).
In your above code, not even your loader be called. It will be called if you initiate App\App class object after its declaration. and If your loader does not work fine then afterwards you will possibly get this error.
Fatal Error: Silex\Application class not found
Please checkout some examples and findings.
Example 1 Here loader is expected to be called but not called, because you have registered after initialization of class($app = new App();).
Example 2 Here, calling class will look for autoloading class because here initialization takes place after registration of loader and declaration of class, which is probably answers your question.
Change your code with this to get it fix:
<?php
namespace App;
require_once __DIR__ . '/../vendor/autoload.php';
class App extends \Silex\Application {
public function __construct()
{
parent::__construct();
echo 'Worked!';
}
}
$app = new App();

Can't find PHP class after implementing namespaces

I am dealing with an old PHP class called Config in a Config.class.php file.
Recently, I have implemented namespaces:
<?php
namespace Tfr\Partners\Smartfocus;
use Tfr\BaseApp;
class Config
{
...
} else {
$isProd = BaseApp::isProd();
}
...
When I am running the code, I now get the following error message:
Fatal error: Class 'Tfr\BaseApp' not found in
<some_path>/Partners/Smartfocus/Config.class.php on line 45
but the BaseClass is there in a file called <some_path>/BaseApp.class.php. However, it does not have a namespace itself.
Unfortunately, it is used in many places in a large code base, and adding a namespace to BaseClass seems very risky.
How can I 'import' BaseClass in Config safely and get rid of my error?

how to use include_once() with namespace in laravel

I am new in laravel and importing a existing php site.
I created a controller named "List" then i need to create a object of a class, coded in a file which is been include by include_once() as shown,
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
But i am getting an error like
Fatal error: Class 'App\Http\Controllers\ServiceDetails' not found
I dont have much idea of namespace "use" and "as".
May be that's why i am not able to solve this problem.
When creating a new object it is searching class in namespace location only, but it should also look in included files, i think.
If there's a namespace in current file, PHP will try to find class in current namespace and if it won't find it, you'll get fatal error. You should open ServiceDetails.class.php class to verify if there is namespace ...; at the beginning of the file (after <?php). If not you can simple add in your Lists file after:
use App\Http\Controllers\Controller;
the following line:
use ServiceDetails
and if it is, you should copy that namespace and add the following line:
use namespaceyoucopied\ServiceDetails;
of course in namespaceyoucopied place you need to put the correct copied namespace so it could look like this:
use A\B\C\ServiceDetails;
You can also look at How to use objects from other namespaces and how to import namespaces in PHP or PHP namespaces manual
You just need to add a use statement for that class so the class in the current file can "see" it.
<?php
namespace App\Http\Controllers;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
use App\Http\Controllers\Controller;
use Namespace\To\ServiceDetails;
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
However, if you are using Laravel and doing this, then you are not using the autoloading feature to its fullest. I recommend you put this file in a namespaced directory in your application and have it follow PSR-4. Then Laravel will load this for you, and it will keep your class file looking clean.
Put the file in a path like the following: /path/to/projectRoot/app/Lib/ServiceDetails.php. Then make the file look like below so it follows PSR-4:
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Lib\ServiceDetails;
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}

PHP Symfony- How to inherit multiple classes from one php file

I am using Symfony 2.4
I have a PHP file OAuth.php which has multiple classes inside that.
Code of OAuth.php is as below
<?php
namespace RT\AuthBundle\DependencyInjection\Vzaar;
use Exception;
class OAuthException extends Exception {
// pass
}
class OAuthConsumer {
//some code
}
class OAuthToken {
//some code
}
?>
I am inheriting above file in Vzaar.php file which is with in same namespace and it's code is as below
<?php
namespace RT\AuthBundle\DependencyInjection\Vzaar;
/*require_once 'OAuth.php';
require_once 'HttpRequest.php';
require_once 'AccountType.php';
require_once 'User.php';
require_once 'VideoDetails.php';
require_once 'VideoList.php';
require_once 'UploadSignature.php';*/
use RT\AuthBundle\DependencyInjection\Vzaar\OAuth;
use RT\AuthBundle\DependencyInjection\Vzaar\HttpRequest;
use RT\AuthBundle\DependencyInjection\Vzaar\AccountType;
use RT\AuthBundle\DependencyInjection\Vzaar\User;
use RT\AuthBundle\DependencyInjection\Vzaar\VideoDetails;
use RT\AuthBundle\DependencyInjection\Vzaar\VideoList;
use RT\AuthBundle\DependencyInjection\Vzaar\UploadSignature;
//use RT\AuthBundle\DependencyInjection\Vzaar\OAuth\OAuthConsumer;
Class Profile
{
public static function setAuth($_url, $_method = 'GET')
{
$consumer = new OAuthConsumer('', '');
//some code
}
}
?>
Creating new object of OAuthConsumer class throws error that
Fatal error: Class 'RT\AuthBundle\DependencyInjection\Vzaar\OAuthConsumer' not found in /var/temp/rt-web/src/RT/AuthBundle/DependencyInjection/Vzaar/Vzaar.php
1/1 ClassNotFoundException: Attempted to load class "OAuthConsumer" from namespace "RT\AuthBundle\DependencyInjection\Vzaar" in /var/temp/rt-web/src/RT/AuthBundle/DependencyInjection/Vzaar/Vzaar.php line 378. Do you need to "use" it from another namespace?
Because since 2.1 Symfony uses composer, and composer uses PSR-0/PSR-4 to autoloading the classes, you have to follow the namespace convention, so you have to create those classes in separate files and in the right directory structure, what is represented in the namespace.
EDIT
As Cerad mentioned in the comment, you can specify a class map in composer, although what is considered as a best practice with symfony projects, create every class in a separate file.

Categories