Autoload error in php - php

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?

Related

Class 'Dropbox\AppInfo' not found - PHP

My code, script start.php in app folder:
require __DIR__ . '../../dropbox-php-sdk-master/vendor/autoload.php';
session_start();
$_SESSION['user_id'] = 1;
$dropboxKey = '';
$dropboxSecret = '';
$appName = '';
$appInfo = new Dropbox\AppInfo($dropboxKey,$dropboxSecret);
My folder structure:
1.app
1.1. start.php
2.dropbox-php-sdk-master
2.1.vendor
2.1.1. autoload.php
3.index.php (calls function start.php)
And when I call the index.php i recive this erro:
Fatal error: Uncaught Error: Class 'Dropbox\AppInfo' not found in C:\xampp\htdocs\tutorials\dropboxapi\app\start.php:12 Stack trace: #0 C:\xampp\htdocs\tutorials\dropboxapi\index.php(3): require() #1 {main} thrown in C:\xampp\htdocs\tutorials\dropboxapi\app\start.php on line 12
I follow some tutorials on the internet and they all have the same structure, what Im doing bad?

PHP script can't work with autoloaded classes

The UsersView class object can't be loaded with the custom autoloader, finishing the PHP script with the following error. What would be the solution?
Fatal error: Uncaught Error: Class 'UsersView' not found in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php:4 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php on line 4
Project structure:
classes
|_____ UsersView.php
common
|_____ autoloader.php
|_____ registration
|_____ pendings.php
Pendings.php script
include("../autoloader.php");
$pendingsView = new UsersView();
$rows = $pendingsView->getAllUsers();
Autoloader.php script
spl_autoload_register('myAutoLoader');
function myAutoLoader($className) {
$path = "../classes";
$extension = ".php";
$fullPath = $path . $className . $extension;
if(!file_exists($fullPath)) {
return false;
}
include_once $fullPath;

phantomJs permission denied

Have a question and I have the feeling it should not be to hard to solve but I'm not really familiar with Phantom Js.
$this->phantomProcess($path)->setTimeout(10)->mustRun();
This line results in the following error:
protected function captureImage($view)
{
$path = $this->writeFile($view);
$this->phantomProcess($path)->setTimeout(10)->mustRun();
return $path;
}
protected function writeFile($view)
{
file_put_contents($path = 'storage/' . md5(uniqid()) . '.pdf' , $view);
return $path;
}
protected function phantomProcess($path)
{
return new Process('bin/phantomjs capture.js ' . $path);
}
I'm running the following line:
Fatal error: Uncaught exception 'Symfony\Component\Process\Exception\ProcessFailedException' with message 'The command "bin/phantomjs capture.js storage/7c5b791a0c1f131aac5c5ca272a1c891.pdf" failed. Exit Code: 126(Invoked command cannot execute) Working directory: /Applications/XAMPP/xamppfiles/htdocs/pdf Output: ================ Error Output: ================ sh: bin/phantomjs: cannot execute binary file ' in /Applications/XAMPP/xamppfiles/htdocs/pdf/vendor/symfony/process/Process.php:239 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/pdf/app/Codecourse/Capture/Capture.php(45): Symfony\Component\Process\Process->mustRun() #1 /Applications/XAMPP/xamppfiles/htdocs/pdf/app/Codecourse/Capture/Capture.php(24): Codecourse\Capture\Capture->captureImage('<!DOCTYPE html>...') #2 /Applications/XAMPP/xamppfiles/htdocs/pdf/index.php(17): Codecourse\Capture\Capture->load('invoice.php', Array) #3 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/pdf/vendor/symfony/process/Process.php on line 239
Anybody who can help me out?
Really appreciated,

error Call to a member function get_results()

error:
Call to a member function
get_results() on null in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php on line
the code
global $wpdb;
$query = $wpdb->get_results('SELECT * FROM test');
The folder myplugin contains the following two files,
index.php
viwe.php
For file viwe.php, the code is not working and fails with,
error
Call to a member function
get_results() on null in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php on line
But the index.php is working.
Add these two lines at the top.
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-load.php');
You need to include those files so the function get_results() becomes available to be called.

Composer psr-4 autoload cannot found file

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

Categories