is_callable loads 'pcu2phmmr6pam3' via __autoload - php

I've got a weird problem going on. I'm developing my website localhost on a xampp server before uploading it to my host server. I finally finished the basic components of my website, so that it could load the homepage. Everything worked as planned.
So I decided to upload it to the host server, then I set the MySQL credentials, now it should work. Ehm.. no, it didn't. Empty page.
So I put on my gloves and started digging into the code with random expressions as
echo 'test'; so I could keep track of what was going on. It seems to run just fine until is_callable() was executed.
is_callable() should run __autoload($class), so I tried var_dump($class)
It gave me this result:
string(11) "Initializer"
string(8) "Database"
string(14) "PageController"
string(14) "BaseController"
string(14) "pcu2phmmr6pam3"
string(14) "pcu2phmmr6pam3"
Now, every class listed here should be there, besides the last two. I have NO idea where that name came from, because it's not a string I've set anywhere.
The only result Google showed me for pcu2phmmr6pam3 was another website having a similar problem.
Now the really weird stuff happens, my autoload function looks like this:
function __autoload($class) {
var_dump($class);
if (file_exists(ROOT_PATH . DS . 'site' . DS . 'class' . DS . $class . '.class.php')) {
require_once(ROOT_PATH . DS . 'site' . DS . 'class' . DS . $class . '.class.php');
} else if (file_exists(ROOT_PATH . DS . 'site' . DS . 'controller' . DS . $class . '.class.php')) {
require_once(ROOT_PATH . DS . 'site' . DS . 'controller' . DS . $class . '.class.php');
} else if (file_exists(ROOT_PATH . DS . 'site' . DS . 'model' . DS . $class . '.class.php')) {
require_once(ROOT_PATH . DS . 'site' . DS . 'model' . DS . $class . '.class.php');
} else{
throw new Exception('Class `' . $class . '` could not be loaded!');
}
}
Every class it should load, is loaded. If I want to create a class which doesn't exist, then it throws an exception.
But with the pcu2phmmr6pam3 'classes' neither is the case. No exceptions were thrown, and now errors were printed on the screen, even though I've set error_reporting(E_ALL)
Here's the surrounding code of is_callable():
$controllerName = ucfirst($this->structure) . 'Controller';
$action = strtolower(((!empty($this->uri[1]))?$this->uri[1]:'index'));
if (is_callable(array($controllerName, $action))) {
$controller = new $controllerName($this->uri, $this->database, $this->structure, $action, $page);
$controller->$action();
} else if (is_callable(array($controllerName, 'index'))) {
$controller = new $controllerName($this->uri, $this->database, $this->structure, 'index', $page);
$controller->index();
} else {
$controller = new NotfoundController($this->uri, $this->database, 'Notfound', 'index', $page);
$controller->index();
}
Last bit of information I can give you:
My localhost xampp server runs PHP 5.4.7, and my host server runs PHP 5.3.20.
Solved, not sure how the weird classnames appeared, if someone knows, I'd like to know why :)

I had a similar problem calling a static method. (same with spl_autoload_register())
function __autoload ($className) {
echo $className."\n";
}
class ClassName {
function functionName () {
}
}
//calls __autoload twice with class name 'a22h1pd_t'
is_callable( array('ClassName', 'functionName') );
//doesn't call __autoload
is_callable('ClassName::functionName');
Looking at this minimal example it's obvious. The static keyword for functionName is missing. Adding it resolved the problem for me with both syntaxes behaving as expected.

Related

Getting error using $this when not in object context to all methods in a class except __construct() method

I have two classes in separate files. The names of classes and files are: Substitute.php and Models.php. I have some properties in class Substitute.php which I can access using $this->property_name in the __construct() method without any error. However when I try to access the same property in the another method(method test() in my case(see the code below)) in the same class (i.e., class Substitute) I get the error Fatal error: Uncaught Error: Using $this when not in object context in G:\ROHAN\eTh0\VectorVolunteers\vector\backend\models\Substitute.php:22. Line 22 being echo $this->c_day; in the method sss() (I have cut some of the code in the class, adding only the which I thought to be relevant, I'll add rest of the code in case someone requires). I access methods sss() and test() from the url by autoloader method and class Router which I have included below too:
Below this line lies the code inside Substitute.php:
EDIT1: I have added another method from Substitute class where also I get the same error.
class Substitute extends Models{
public $user_id, $c_day, $c_date;
public function __construct(){
$_SESSION['username']='user2';
echo 'constructor in class Substitute ran';
$this->user_id = $this->get_user_id();
$this->c_day = $this->upcoming_class();
}
public function sss(){
echo $this->c_day;
}
public function test(){
if(isset($_POST['select_class_subs'])){
switch ($_POST['select_class_subs']) {
case 'Next Class':
$this->c_date = date('d-M-yy', strtotime(($this->c_day)));
break;
case 'Next-to-next Class':
$this->c_date = date('d-M-yy', strtotime(($this->c_day . '+1 week')));
break;
default:
echo 'Custom class selected <br>';
break;
}
if($this->c_date==date('d-M-yy')){
echo "Sorry, but you cannot post a substitute request for a class that is scheduled to happen on the same day. <br>";
}
else{
echo "Done! <br>";
}
}
}
EDIT2: The code in the file config.php which is required in the autoloader() method(See below):
if(isset($_GET['url'])){$url = explode('/', $_GET['url']);}
require_once (ROOT . DS . 'backend' . DS . 'bootstrap.php');
EDIT2: The code of file bootstrap.php which has autloader() method:
require_once 'config.php';
// Autoloader for classes
spl_autoload_register('autoloader');
function autoloader($class_Name){
if (file_exists (ROOT . DS . 'backend' . DS .'core' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'core' . DS . $class_Name . '.php');
}
elseif (file_exists (ROOT . DS . 'backend' . DS .'models' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'models' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'views' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'views' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'controllers' . DS . $class_Name . '.php')){
require_once (ROOT . DS . 'backend' . DS . 'controllers' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php')) {
require_once (ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php');
}
elseif (file_exists(ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php')) {
require_once (ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php');
}
else {echo 'Class does not exist or Class file not found' . '<br>';}
}
// Route the request to router.php
if (isset($_GET['url'])) {
Router::route($url);
}
EDIT2: Code in the file that contains class Router{}:
class Router{
private static $controller_name, $method_name;
public static function route($url){
if (isset($url[0]) && $url[0] != "") {
$controller = ucwords($url[0]);
self::$controller_name = $controller;
array_shift($url);
}
if (isset($url[0]) && $url[0] != "") {
$method = ucwords($url[0]);
self::$method_name = $method;
array_shift($url);
}
$params = $url;
$init = new self::$controller_name;
if (method_exists(self::$controller_name, self::$method_name)){
call_user_func_array([self::$controller_name, self::$method_name], $params);
}
else{
if(self::$method_name!=NULL){
echo 'The requested method "' . self::$method_name . '" does not exist in ' . '"' . self::$controller_name . '" controller.';
}
}
}
Below this lies the code inside Models.php (I have included it in case someone requires. But know that I have extended this class to many other classes and all of the methods in this class work fine without any errors or warning).
class Models extends Database {
private $db, $user_id, $table, $rv_id_table_name, $av_id_table_name, $class_day;
protected function get_user_id(){
if (isset($_SESSION['username'])){
$username=$_SESSION['username'];
$sql = 'SELECT * FROM volunteers where username=:username';
$params = [
'username' => $username
];
$array = $this->query($sql, $params);
$this->user_id = $array['id'];
return $this->user_id;
}
else{
echo 'You are not signed in.' . '<br>' . 'Please sign in first.';
}
}
protected function upcoming_class(){
if (isset($_SESSION['username'])) {
$id = $this->get_user_id();
$params = [
'id' => $id
];
$sql = 'SELECT class_day FROM volunteers where id=:id';
$this->class_day = $this->query($sql, $params);
return $this->class_day['class_day'];
}
}
I want to know why am I getting the above mentioned error (in Italics) and how to remove it. I asked it because I have been trying since yesterday, with no success.
How do you use that method (sss)? Are you by chance calling it statically:
Substitute::sss();
If so, that is why you are having the issue. The method is not static. It must be called like this (as one example):
$object = new Substitute();
$object->sss();
I tried converting the properties $c_day and $c_date from class Substitute{} from public to public static and instead of using $this-> to above properties I used self:: in the methods public function sss() and public function test() and to my surprise it works. No more errors.

Class not found in Bootstrap.php in CLI mode (Zend)

I am getting an error when trying to run app in CLI mode. First of all I have daemon controller which allows to run daemon instances (objects with only one required method 'run' derived from my daemon interface) using url /daemon-cli/:Daemon_Class_To_Run. So there is nothing interesting from this side.
Also I have little sh script to run this controller in a simplified way like this:
# ./daemon.sh Ami_Daemon_EventParser
This returns me the next error:
<?
class System_Controller_Plugin_ModuleLayoutLoader extends Zend_Controller_Plugin_Abstract {
/**
* Array of layout paths associating modules with layouts
*/
protected $_moduleLayouts;
// here was the full code of this class
}
PHP Fatal error: Class 'System_Controller_Plugin_ModuleLayoutLoader' not found in /var/www/htdocs/application/Bootstrap.php on line 99
System namespace is located in application/library/System and this is just a set of toolkits and libraries. That is completely weird because it works well in apache2handler mode, but crashes in cli. I don't understand how the class cannot be found if the code of "not founded" class are returned in the error. I suppose I bootstrapped application in a wrong way.
Some of bootstrap code:
protected function _initAppModules()
{
$modules = array();
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('System_');
$includePath = '';
// Add model directories to include path
if ($dhApp = opendir(APPLICATION_PATH . '/modules')) {
while (($dirApp = readdir($dhApp)) !== false) {
if (substr($dirApp, 0, 1) != '.' && is_dir(APPLICATION_PATH . '/modules/' . $dirApp)) {
$modules [] = $dirApp;
if ($dhModule = opendir(APPLICATION_PATH . '/modules/' . $dirApp)) {
while (($dirModule = readdir($dhModule)) !== false) {
if ($dirModule == 'library' && is_dir(APPLICATION_PATH . '/modules/' . $dirApp . '/library')) {
$includePath .= PATH_SEPARATOR . APPLICATION_PATH . '/modules/' . $dirApp . '/library';
$autoloader->registerNamespace(ucfirst($dirApp));
}
}
closedir($dhModule);
}
}
}
closedir($dhApp);
}
ini_set('include_path', ini_get('include_path') . $includePath);
return $modules;
}
protected function _initResources()
{
Zend_Controller_Action_HelperBroker::addPrefix('System_Controller_Action_Helper');
$resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH . '/library/System', 'namespace' => ''));
$resourceLoader->addResourceTypes(array('form' => array('path' => 'Form/Resource/', 'namespace' => 'Form'), 'model' => array('path' => 'Model/Resource/', 'namespace' => 'Model')));
System_API_Abstract::load();
return $resourceLoader;
}
protected function _initView(array $options = array())
{
$this->bootstrap('AppModules');
$this->bootstrap('FrontController');
$view = new Zend_View();
$view->addScriptPath(APPLICATION_PATH . '/views/scripts');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'View_Helper');
$view->addHelperPath(APPLICATION_PATH . '/modules/gui/views/helpers', 'View_Helper');
$view->addHelperPath(APPLICATION_PATH . '/library/System/View/Helper', 'System_View_Helper');
// next line triggers the error
$layoutModulePlugin = new System_Controller_Plugin_ModuleLayoutLoader();
foreach ($this->getResource('AppModules') as $module) {
if (is_dir(APPLICATION_PATH . '/modules/' . $module . '/views/layouts')) {
$layoutModulePlugin->registerModuleLayout($module, APPLICATION_PATH . '/modules/' . $module . '/views/layouts');
}
if (is_dir(APPLICATION_PATH . '/modules/' . $module . '/views/helpers')) {
$view->addHelperPath(APPLICATION_PATH . '/modules/' . $module . '/views/helpers', ucfirst($module) . '_View_Helper');
}
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
Zend_Layout::startMvc(array('layoutPath' => '../application/views/layouts', 'layout' => 'layout'));
$this->getResource('FrontController')->registerPlugin($layoutModulePlugin);
return $view;
}
And in the index.php I bootstrap all modules.
if (php_sapi_name() == 'cli') {
$_SERVER['REQUEST_URI'] = $argv[1];
$_SERVER ['HTTP_HOST'] = 'mydomain.info';
ini_set('session.use_cookies', false);
$application->bootstrap()->run();// in case if I find the way to bootstrap partially
} else {
$application->bootstrap()->run();
}
I am fighting with this for a couple of weeks, so I hope somebody has an answer or a good advice. Any help is greatly appreciated. Thanks!
Finally I did that!
As you can see I had a php short tag in the error output. Php often uses separate php.ini for cli mode as my rackspace distributive does. Php distros for windows usually have the common ini file.

!class_exists is not working in magento?

When I run test.php, why always error on !class_exists line?
This is test.php:
<?php //test.php
require_once './app/Mage.php';
Mage::app()->setCurrentStore(0);
Mage::setIsDeveloperMode(true);
require_once("test-class.php");
?>
This is test-class.php:
<?php //test-class.php
if (!class_exists("AClass")) {
class AClass {
public function AnAction() {
return 123;
}
}
}
?>
Because the Magento bootstrap app/Mage.php registers an autoloader, your call to class_exists() triggers attempts to load a class definition for this class. This behavior can be changed by passing false:
<?php //test-class.php
if (!class_exists("AClass",false)) {
class AClass {
public function AnAction() {
return 123;
}
}
}
?>
Further, the bootstrap sets up include path arguments for use by the autoloader:
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
$paths[] = BP . DS . 'lib';
Placing your class definition in any of the above directories will allow it to be defined whenever a definition is required.

PHP Autoload ignorecase

Is there any way to call require_once with some "case insensitive flag" ?
In windows it's okay, but linux is case sensitive. Is there any way to override ?
Thanks
Sure, load
strtolower($className . ".php")
and name your files in lowercase.
Regardless of how you try to load your files, only the lowercase version will ever be loaded.
Just include some where else in you header.php or some other common file.,
<?php
$filename = basename($_SERVER['SCRIPT_FILENAME']);
$request = basename($_SERVER['SCRIPT_NAME']);
if($filename != $request)
die('Case of filename and request do not match!');
?>
I think this may help you resolve your problem. also refer the following location https://superuser.com/questions/431342/linux-both-case-sensitive-and-case-insensitive-and-always-inconvenient
You can use this every time you are loading you auto load the appropriate classes. yu have to change the directories depends on you project. you can you the echo or print_r to print what classes are loaded every time when you are calling something. also all you class names must ot have the same format for example, className.class.php e.g Dashboard.class.php, Category.class.php. you can use
ucwords to make the first letter capital.
function __autoload($className)
{
if (file_exists(__DIR__ . '/../library/' . strtolower($className) . '.class.php'))
{
require_once(__DIR__ . '/../library/' . strtolower($className) . '.class.php');
}
else if (file_exists(__DIR__ . '/../application/controllers/' . strtolower($className) . '.php'))
{
require_once(__DIR__ . '/../application/controllers/' . strtolower($className) . '.php');
}
else if (file_exists(__DIR__ . '/../application/models/' . strtolower($className) . '.php'))
{
require_once(__DIR__ . '/../application/models/' . strtolower($className) . '.php');
}
}

Strange behaviour for spl_autoload on phpfog

I'm just trying to build my first app on PHP Fog but there's a piece of code that doesn't run properly - works fine on localhost and other regular hosts though.
I use a modified version of TinyMVC, this is the code responsible for setting up autoloading:
/* Set include_path for spl_autoload */
set_include_path(get_include_path()
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'core' . DS
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'libraries' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'controllers' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'models' . DS
);
/* File extensions to include */
spl_autoload_extensions('.php,.inc');
/* Setup __autoload */
$spl_funcs = spl_autoload_functions();
if($spl_funcs === false)
spl_autoload_register();
elseif(!in_array('spl_autoload',$spl_funcs))
spl_autoload_register('spl_autoload');
Basically, it fails at the first class it should load, which is located in "FRAMEWORK_BASEDIR . 'core' . DS". The class filename is "framework_controller.php" and class name is "Framework_Controller" (tried lowercase as well). If I include the class manually it works but fails with autoload.
Here's the error message that I get:
Fatal error: spl_autoload(): Class Framework_Controller could not be loaded in /var/fog/apps/app7396/claudiu.phpfogapp.com/application/controllers/home.php on line 12
Any ideas as to what could the problem be?
I managed to sort it out:
function framework_autoload($className, $extList='.inc,.php') {
$autoload_paths = array (
FRAMEWORK_BASEDIR . 'core' . DS,
FRAMEWORK_BASEDIR . 'libraries' . DS,
FRAMEWORK_APPLICATION . DS . 'controllers' . DS,
FRAMEWORK_APPLICATION . DS . 'models' . DS
);
$ext = explode(',',$extList);
foreach($ext as $x) {
foreach ($autoload_paths as $v) {
$fname = $v . strtolower($className).$x;
if(#file_exists($fname)) {
require_once($fname);
return true;
}
}
}
return false;
}
spl_autoload_register('framework_autoload');
Thanks to another question here on StackOverflow: spl_autoload problem

Categories