Liquid Fire Posted April 12, 2007 Share Posted April 12, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/ Share on other sites More sharing options...
corbin Posted April 12, 2007 Share Posted April 12, 2007 Let's pretend that class is called Class1. From what I understand, it would include class_a.php if you called: $class = New Class1("a"); Is that right, or are you trying to do something else? Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-227997 Share on other sites More sharing options...
Liquid Fire Posted April 12, 2007 Author Share Posted April 12, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228001 Share on other sites More sharing options...
corbin Posted April 12, 2007 Share Posted April 12, 2007 require_once("class_{$class}.php") Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228004 Share on other sites More sharing options...
Glyde Posted April 12, 2007 Share Posted April 12, 2007 Take it out of the string. require_once("class_" . $class . ".php"); Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228006 Share on other sites More sharing options...
Liquid Fire Posted April 12, 2007 Author Share Posted April 12, 2007 corbin: that is what i currently have and it is giving me class_.php and not class_formfieldcreator.php Glyde: i have tried that too, same result. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228017 Share on other sites More sharing options...
Glyde Posted April 12, 2007 Share Posted April 12, 2007 Try this: $className = "class_"; $className .= $class; $className .= ".php"; require_once $className; Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228024 Share on other sites More sharing options...
kenrbnsn Posted April 12, 2007 Share Posted April 12, 2007 Have you made sure that the variable "$class" has anything in it? Put in some "echo" statements to display what it contains upon entering the function and after the substr() function. Ken Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228027 Share on other sites More sharing options...
Liquid Fire Posted April 12, 2007 Author Share Posted April 12, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228030 Share on other sites More sharing options...
Liquid Fire Posted April 12, 2007 Author Share Posted April 12, 2007 Glyde: you second suggestion give me the same result. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228033 Share on other sites More sharing options...
Liquid Fire Posted April 12, 2007 Author Share Posted April 12, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228048 Share on other sites More sharing options...
per1os Posted April 12, 2007 Share Posted April 12, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228051 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 the auto_laod file is in the same place as all other class but that is not even the problem, for some reason when i have the"." in there is does not read the $class variable into that string. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228061 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228066 Share on other sites More sharing options...
Guest prozente Posted April 13, 2007 Share Posted April 13, 2007 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'); ?> Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228069 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 i have the lastest version 5.2.1 Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228096 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228099 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228101 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 I found my error, it was in my formfieldcreator class. class_exist is the other function the was invoking the __autoload function i was using the wrong variable and was formatting the variable after i i did the class_exist. Link to comment https://forums.phpfreaks.com/topic/46775-weird-result-when-adding-a-variable-and-string/#findComment-228136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.