Im using this library https://serp-spider.github.io/documentation/search-engine/google/
When I follow their example I got this.
Fatal error: Uncaught Error: Class "Serps\SearchEngine\Google\GoogleClient" not found in /var/www/html/index.php:19 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 19
This is my index.php
<?php
use Serps\SearchEngine\Google\GoogleClient;
use Serps\SearchEngine\Google\GoogleUrl;
$googleClient = new Serps\SearchEngine\Google\GoogleClient($httpClient);
$googleUrl = new GoogleUrl();
$google->setSearchTerm('simpsons');
$response = $googleClient->query($googleUrl);
$results = $response->getNaturalResults();
foreach($results as $result){
// Here we iterate over the result list
// Each result will have different data based on its type
}
?>
I've been thinking about what it could be for a while but I can't
I was missing the autoload, I added this line and works.
require 'vendor/autoload.php';
I changed the namespace and forgot to update it in composer.json
"autoload": {
"psr-4": {
"MyNamespace\\": "src"
}
},
Then run composer dump-autoload
Related
I was trying to test BladeOne using PHP7.3.
composer.json
{
"name": "TEST",
"require": {
"eftec/bladeone": "^3.33"
},
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
}
test.php
require "vendor/autoload.php";
Use eftec\bladeone;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
$blade = new BladeOne($views,$cache,BladeOne::MODE_AUTO);
// $blade -> setAuth( ' johndoe ' , ' admin ' );
echo $blade->run("hello",array("variable1"=>"value1"));
This error appears when you run it.
Fatal error: Uncaught Error: Class 'eftec\bladeone' not found in /*/test.php on line 8
Error: Class 'eftec\bladeone' not found in /*/test.php on line 8
I also found a link like this but it didn't work.
Why does this error occur?
Replace Use eftec\bladeone; to use eftec\bladeone\BladeOne;.
And remove:
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
From your composer.json.
Hope help you.
You included only the namespace, not the class name
Change your Use statement by this one :
use eftec\bladeone\BladeOne;
Or instantiate the class like this :
$blade = new bladeone\BladeOne($views,$cache,BladeOne::MODE_AUTO);
(Don't do both)
I installed Foolz SphinxQL Query Builder for PHP with composer using the following json file:
{
"require": {
"foolz/sphinxql-query-builder": "^2.0"
}
}
My php is as follows:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Foolz\SphinxQL\SphinxQL;
use Foolz\SphinxQL\Connection;
error_reporting(E_ALL);
ini_set('display_errors', 1);
// create a SphinxQL Connection object to use with SphinxQL
$conn = new Connection();
$conn->setConnectionParams('127.0.0.1', 9306);
$query = SphinxQL::create($conn)->select('*')
->from('test1')
->match('#test document');
# ->where('banned', '=', 1);
$result = $query->execute();
var_dump($result);
?>
Using my debugger I see the autoloader (function findFileWithExtension) is trying to find the file at /mnt/i/var/www/vhosts/my.play.net/sphinx/vendor/composer/../foolz/sphinxql-query-builder/Connection.php when it should presumably be looking in /mnt/i/var/www/vhosts/my.play.net/sphinx/vendor/composer/../foolz/sphinxql-query-builder/Drivers/Mysqli/Connection.php where it is actually located.
Can anyone advise why I might be seeing this and how I fix it?
You're using incorrect namespace. To get vendor/foolz/sphinxql-query-builder/src/Drivers/Mysqli/Connection.php you need to use Foolz\SphinxQL\Drivers\Mysqli\Connection as FQN:
use Foolz\SphinxQL\SphinxQL;
use Foolz\SphinxQL\Drivers\Mysqli\Connection;
I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?
I am still learning to use php composer
i have a directory structure like this :
Directory Structure
and this is my composer.json
{
"autoload": {
"psr-4": {
"Kct\\": "lib/"
}
}
}
Now in my index.php file i am trying to load Class tes in tesdir.php
<?php
// file: index.php
require __DIR__ . '/vendor/autoload.php';
$x = new \Kct\Tesdir\Tes();
var_dump($x->tes()); //output: 'GET'
my tesdir.php :
<?php
namespace Kct\Tesdir;
class Tes {
public function tes() {
return $_SERVER['REQUEST_METHOD'];
}
}
now if I open index.php in my localhost I got and error like this :
Fatal error: Uncaught Error: Class 'Kct\Tesdir\Tes' not found in /var/www/html/tesComposer/index.php:6 Stack trace: #0 {main} thrown in /var/www/html/tesComposer/index.php on line 6
can someone explain why.?
tesdir.php should be named Tes.php. The name of the file should match the name of the class.
See the PSR-4 examples
I am trying to register my own class as a services with help of symfony dependency injection component, but i have problems with class loading.
I have file structure as this:
My Generator class is simple
<?php
namespace Localhost\Service\String;
class Generator {
private $iStringLength;
public function __construct($iNewStringLength = 5) {
$this->iStringLength = $iNewStringLength;
}
public function getRandomString() {
$sChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$sRandChar = substr(str_shuffle(str_repeat($sChars,5)),0, $this->iStringLength);
return $sRandChar;
}
}
And Index is
<?php
require_once 'vendor/autoload.php';
/*
spl_autoload_register(function ($sClass) {
echo $sClass;
require_once str_replace('\\', '/', $sClass) . '.php';
});
*/
use Localhost\Service\String\Generator;
/*
$oStringGenerator = new Generator(55);
echo $oStringGenerator->getRandomString();
*/
use Symfony\Component\DependencyInjection\ContainerBuilder;
$oContainer = new ContainerBuilder();
$oContainer
->register('generator', 'Generator')
->addArgument('15');
$oGeneratorService = $oContainer->get('generator');
echo $oGeneratorService->getRandomString();
What i am getting is an error
Fatal error: Uncaught exception 'ReflectionException' with message 'Class Generator does not exist' in D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php:959 Stack trace: #0 D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php(959): ReflectionClass->__construct('Generator') #1 D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php(493): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'generator') #2 D:\Localhost\Apache\htdocs\Test\index.php(26): Symfony\Component\DependencyInjection\ContainerBuilder->get('generator') #3 {main} thrown in D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php on line 959
Or as a picture
$oContainer = new ContainerBuilder();
$oContainer
->register('generator', 'Localhost\Service\String\Generator')
->addArgument('15');
Solution is simple, i forgot to modify composer config to load my services
"autoload": {
"psr-0": {"Localhost": "src/"}
},
Edit: Since Symfony 3.3+ (May 2017) you can use register() class name service shortcut:
$containerBuilder = new ContainerBuilder();
$containerBuilder->register(Localhost\Service\String\Generator::class)
->addArgument('15');
Since PHP 5.5+ you can use more fail-proof ::class notation:
$containerBuilder = new ContainerBuilder();
$containerBuilder->register('generator', Localhost\Service\String\Generator::class)
->addArgument('15');
Now, when class name will be miss-typed, your IDE will highlight it.
addition:
You should propbably compile the container for performance reasons.
$container = new ContainerBuilder();
$container
->register('generator', 'Localhost\Service\String\Generator')
->addArgument('15')
;
$container->compile();