Jump to content

Weird result when adding a variable and string


Liquid Fire

Recommended Posts

take the following code

function __autoload($class)
{
$class = strtolower(substr($class, 1));

//$class_file = "class_" . $class . ".php";

require_once("class_{$class}.php");
}

this will return the following error:

 

Fatal error: __autoload() [function.require]: Failed opening required 'class_.php'

 

this is because for some reason the "." in the ".php"is messing it up because if i have this:

function __autoload($class)
{
$class = strtolower(substr($class, 1));

//$class_file = "class_" . $class . ".php";

require_once("class_{$class}php");
}

 

I get the expected error:

 

Fatal error: __autoload() [function.require]: Failed opening required 'class_formfieldcreatorphp'

 

why would the "." in the ".php" part of the sting mess this up, i mean i have done a __autoload function before and never had this problem.  there has to be something so simple and when someone points it out to me i am going to fell like a dumbass but i just don't see anything wring there.

the way i name my classes is begin with cap "C" and the each word starts with caps so my class for my form field creator is called:

 

CFormFieldCreator

 

the way i name my file and any class file starts with"class_" and the the class name without the "C" all in lowercase like:

 

class_formfieldcreator.php

 

now "class_{$class}.php" should equal "class_formfieldcreator.php" but it does not but the variable is the $class variable is correct becuase  "class_{$class}php" does = "class_formfieldcreatorphp" which leads to to the onyl thing i see if possable which is the "." character is messign something up.

 

i'll repeat:

 

now "class_{$class}.php" should equal "class_formfieldcreator.php" but it does not but the variable is the $class variable is correct becuase  "class_{$class}php" does = "class_formfieldcreatorphp" which leads to to the onyl thing i see if possable which is the "." character is messign something up.

anyone else?  this is now my code that is still giving me the same results:

<?php
function __autoload($class_name)
{
//since class names are: CClassName :we need to format the name for the file name: class_classname.php
//remove the first C from the class name
$file_name = substr($class_name, 1);

//make everything to lowercase
$file_name = strtolower($file_name);

require_once("class_{$file_name}.php");
}
?>

 

I copied this code directly from an existing project i have and it is still showing up class_.php instead of class_formfieldcreator.php and the other projects works fine.

Make sure you have the include path right. If that class file is not in the same directory as the script running, well your not going to get anywhere with this.

 

<?php
define('CLASS_PATH', 'classes/');
function __autoload($class_name)
{
//since class names are: CClassName :we need to format the name for the file name: class_classname.php
//remove the first C from the class name
$file_name = substr($class_name, 1);

//make everything to lowercase
$file_name = strtolower($file_name);

require_once(CLASS_PATH . "class_" . $file_name . ".php");
}
?>

 

That would be my suggestion.

now i am really confused:

 

echo "class_{$file_name}.pp" OUTPUTS => class_formfieldcreator.pp

echo "class_{$file_name}.ph" OUTPUTS => class_formfieldcreator.ph

echo "class_{$file_name}.hp" OUTPUTS => class_formfieldcreator.hp

echo "class_{$file_name}php" OUTPUTS => class_formfieldcreatorphp

echo "class_{$file_name}.php" OUTPUTS => class_.pp

 

I don't get why all the others work but the bottom one does not.

Guest prozente

Liquid Fire I'm having no problems with the below code, it works as it should. I'm running PHP 5.2.1, perhaps there was a bug that was fixed in newer PHP versions. You might want to check http://bugs.php.net/search.php for you version of PHP.

 

<?php
error_reporting(E_ALL);

function __autoload($class){

  $class = strtolower(substr($class, 1));
  //$class_file = "class_" . $class . ".php";
  require_once("class_{$class}.php");

}

__autoload('CFormFieldCreator');

?>

well when i make it include_once instead of require_once, i still get the error but the classes work as they should, it looks like it first loads the correct class and then trys to load the "class_.php". this si very wierd man, this work fine on my other project.

the only thing that is different with this project is CFormFieldCreator uses a facktory pattern, here is ther code for it:

<?php
class CFormFieldCreator
{
public function __construct()
{
	//this handles error processing for us
	//$this->_error_handling = new CErrorHandler();
}

/**
* @desc This function will create the form element and return the HTML code needed to display it
* @param string $class name This will tell the function what type of class to create.
* @param mixed[] $attributes This is the parameters to the form field being created
*/
public function CreateField($class_name, $attributes = array())
{
	if(!class_exists($type) || !is_array($attributes))
	{
		//this is the error message we are going to send to the error handler
		$error_message = null;
		if(!class_exists($type))
		{
			$message .= "The class {$type} does not exist.<br />";
		}
		if(!class_exists($type))
		{
			$message .= "The attributes must be passed in an array.";
		}
		//$this->_error_handling->ProcessError($message);
	}

	//create the correct class name
	$class_name = strtolower($class_name);
	$class_name = ucfirst($class_name);
	$class_name = "C" . $class_name . "Field";

	//create the new class with the passed attributes
	$form_field = new $class_name($attributes);

	//return the html needed to display the form field
	return $form_field->HTMLOutput();
}

/**
* @desc Holds the error handling class
*/
private $_error_handling;
}
?>

 

could that be the problem?

 

EDIT:

here is the list of output when i use include and also print out the file name:

 

class_formfieldcreator.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_textfield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_hiddenfield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_buttonfield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_radiofield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_submitfield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_selectfield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_passwordfield.phpclass_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_.php

Warning: __autoload(class_.php) [function.--autoload]: failed to open stream: No such file or directory in D:\root\myframework\classes\auto_load.php on line 13

 

Warning: __autoload() [function.include]: Failed opening 'class_.php' for inclusion (include_path='.;C:\php5\pear') in D:\root\myframework\classes\auto_load.php on line 13

class_checkboxfield.php

 

so as you can see it is loading file like class_checkboxfield.php but there are alot of class_.php file which means that __autoload is being called with now class.  is there anything else besides creating a new class that would call that function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.