$r = set_include_path(get_include_path() . '\google-api-php-client-master\src');
echo $r;
require_once 'Google/Client.php';
require_once 'Google/Service/AdExchangeSeller.php';
function __autoload($class_name) {
include 'examples/' . $class_name . '.php';
}
when I am running it on browser its showing following error..
You are not using the path seperator when calling set_include_path, try this:
$r = set_include_path(get_include_path() . PATH_SEPARATOR . '\google-api-php-client-master\src');
Here is the documentation on set_include_path
Edit
Did you restart Apache after making the change to your php.ini?
Related
I am pretty new to PHP so this may be a dumb question but yeah. I'm trying to get information about a user on instagram with this library: https://github.com/postaddictme/instagram-php-scraper
I've added the library and also the Unirest library so that it should work?
I also have this code in my index.php file:
<?php
require_once dirname(__FILE__) . '/Unirest/Exception.php';
require_once dirname(__FILE__) . '/Unirest/Method.php';
require_once dirname(__FILE__) . '/Unirest/Response.php';
require_once dirname(__FILE__) . '/Unirest/Request.php';
require_once dirname(__FILE__) . '/Unirest/Request/Body.php';
require_once dirname(__FILE__) . '/InstagramScraper/Instagram.php';
require_once dirname(__FILE__) . '/InstagramScraper/Endpoints.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Account.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Comment.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Location.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Media.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Tag.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramException.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramAuthException.php';
require_once dirname(__FILE__) . '/InstagramScraper/Exception/InstagramNotFoundException.php';
use InstagramScraper\Instagram;
$account = Instagram::getAccountById(272308256);
echo $account->username;
?>
But this code does not want to run :/. I keep getting this error:
Fatal error: Using $this when not in object context in
C:\xampp\htdocs\Instagramposts\InstagramScraper\Instagram.php on line 313
I mean if it's a finished library it should immediately work? Anybody's got a solution?
You need to create an instance of the class (object) first before using the method. The method makes use of the "this" operator which needs an object.
Your code should look like this:
use InstagramScraper\Instagram;
$insta = new Instagram;
$account = $insta->getAccountById(272308256);
echo $account->username;
I use of spl_autoload_register for class auto loading like bellow
spl_autoload_register(array($this, 'mainLoader'));
function mainLoader($class) {
$dirs = explode(CLASS_SEPARATOR, $class);
$dirsLen = count($dirs);
$class_name = $dirs[$dirsLen - 1];
if ($dirsLen > 1) {
$paths = array_slice($dirs, 0, $dirsLen - 1);
$path = ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $paths) . DIRECTORY_SEPARATOR;
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
} else {
set_include_path(get_include_path() . PATH_SEPARATOR . ROOT);
}
spl_autoload_extensions(".php");
spl_autoload($class_name);
}
input class name can be class, dir_class, dir_dir_class, ...
and file can be ROOT/class.php Root/dir/class.php , Root/dir/dir/class.php, ...
but when I run program Error to me
Fatal error: Class 'class' not found in ...
why my auto loader don't work correctly??
note: this function work good in Windows but don't work in Linux Ubuntu 14.04
You're probably using camelCase names to your files.
Unix is case sensitive in file names and spl_autoload will work just with lower case.
I have a file that auto loads each of my classes.
This is what it contains:
spl_autoload_register(function($class){
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
require_once 'functions/hash.php';
But when I require_once this file from another php file that is inside my ajax folder, it will try looking for the classes, the function will look from my classes with the path: main_folder/ajax/classes instead of just main_folder/classes.
Does anyone know how to fix this?
FIX:
spl_autoload_register(function($class){
if (file_exists('classes/' . $class . '.php')) {
require_once 'classes/' . $class . '.php';
}
elseif (file_exists('../classes/' . $class . '.php')) {
require_once '../classes/' . $class . '.php';
}
elseif (file_exists('../../classes/' . $class . '.php')) {
require_once '../../classes/' . $class . '.php';
}
You should simple use this function just once - in the main file (usually index.php) and not in another files.
However if it's not possible (but I don't see any reason when could it be not possible) you can change it for example that way:
spl_autoload_register(function($class){
if (file_exists('classes/' . $class . '.php')) {
require_once 'classes/' . $class . '.php';
}
elseif (file_exists( $class . '.php')) {
require_once $class . '.php';
}
});
Here is a proper way, It should universally work.
EDIT: Make sure to specify levels on dirname in order to find the correct path.
spl_autoload_register(function ($classname) {
$file_realpath = dirname(realpath(__FILE__), levels: 1 /* Change that? */) . DIRECTORY_SEPARATOR . dirname($classname);
$classpath = sprintf('%s' . DIRECTORY_SEPARATOR . '%s.php', $file_realpath, basename($classname, '.{php,PHP}'));
if (file_exists($classpath)) {
echo "Loading <b>$classpath</b>...<br>";
require($classpath);
}
});
You need to know the absolute path to the classes directory, then just use a full qualified path to get there.
Make sure your document root is correctly configured with your http server. Furthermore this is usually solved by routing all requests to index.php and deriving the base path (document root) from there. Here is your code modified:
spl_autoload_register(function($class) {
$resolved = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
if(file_exists($resolved)) {
require_once $resolved;
} else {
// make it known to your that a class failed to load somehow
return;
}
})
Here is another implementation that I use for simple projects
We are using a VPS server in our company and I'm trying to install Roundcube webmail interface
But I can't even get to the configuration phase because the set_include_path function doesn't work and the script can't find the required configuration files.
I get an error like "Fatal Error, ini_set/set_include_path function does not work."
I assume some php settings is causing this but I don't which one.
I'd be glad if I could get some help.
Thanks in advance
//EDIT Here is the codes from the script
ini_set('error_reporting', E_ALL&~E_NOTICE);
ini_set('display_errors', 1);
define('INSTALL_PATH', realpath(dirname(__FILE__) . '/../').'/');
define('RCMAIL_CONFIG_DIR', INSTALL_PATH . 'config');
$include_path = INSTALL_PATH . 'program/lib' . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . 'program' . PATH_SEPARATOR;
$include_path .= INSTALL_PATH . 'program/include' . PATH_SEPARATOR;
$include_path .= ini_get('include_path');
set_include_path($include_path);
require_once 'utils.php';
require_once 'main.inc';
I'm doing this from memory, so it might not be quite right, but I think maybe you are confusing the path and directory separators. There may also be a nicer way to do this than what you are doing (i.e. assembling the whole path at once). Try something like this:
define('INSTALL_PATH', dirname(dirname(__FILE__)));
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALL_PATH . DIRECTORY_SEPARATOR . 'program' . DIRECTORY_SEPARATOR . 'lib');
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALL_PATH . DIRECTORY_SEPARATOR . 'program' . DIRECTORY_SEPARATOR . 'include');
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALL_PATH . DIRECTORY_SEPARATOR . 'program');
Usually I compress this a little bit with implode, since DIRECTORY_SEPARATOR is so verbose:
...PATH_SEPARATOR . implode(DIRECTORY_SEPARATOR, Array(INSTALL_PATH, 'program', 'lib'));
I think by (most importantly) changing some of your PATHs to DIRECTORYs, and (possibly) using incremental get_include_path and set_include_path calls, it will be more readable, portable and just might work properly.
function __autoload($class_name) {
echo("Attempting autoload ");
if (substr($class_name, -6) == "Mapper") {
$file = 'mappers/'.$class_name.'.php';
echo "Will autoload $file ";
include_once($file);
}
}
__autoload("UserMapper");
$user = new UserMapper($adapter);
die("done");
Result:
Attempting autoload Will autoload mappers/UserMapper.php done
function __autoload($class_name) {
echo("Attempting autoload ");
if (substr($class_name, -6) == "Mapper") {
$file = 'mappers/'.$class_name.'.php';
echo "Will autoload $file ";
include_once($file);
}
}
//__autoload("UserMapper");
$user = new UserMapper($adapter);
die("done");
(I just commented out the manual call to __autoload()...)
Result:
Fatal error: Class 'UserMapper' not found in C:\Program Files\EasyPHP-5.3.5.0\www\proj\29letters\login.php on line 13
Any ideas?
And yes, I'm running PHP 5.3.5
Not sure why your example isn't working, as it should be as per the manual.
Have you tried using spl_autoload_register to register the autoloader function?
Have you set a proper include_path? You're using a relative path to include the class's file. Try an absolute path instead.
$dir = __DIR__ . '/../path/to/mappers';
$file = $dir . '/' . $class_name . '.php';
require $file;
or
// do this outside of __autoload
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../path/to/mappers';
// inside __autoload
$file = $class_name . '.php';
require $file;