drath Posted December 31, 2008 Share Posted December 31, 2008 I am currently working on a "site checker" that will automatically get information from various .php files on the server. Currently I am using "require()" to include the .php files to get the variables and data from within. The problem is that the information is contained within a class, and by rule, you cannot include/require a .php that uses the same class name, twice. By the very nature of this program, I NEED to use the same class multiple times, and include/require multiple times. Does anybody know any way around this problem? Hopefully I am being clear enough, if not, refer to this diagram: INCLUDE siteone.php uses a class named "site" INCLUDE sitetwo.php uses a class named "site" The following results in "Fatal error: Cannot redeclare class site". I cannot rename the class, it is integral to the actual sites - besides that would defeat the purpose of dynamically checking the .phps. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/ Share on other sites More sharing options...
xtopolis Posted December 31, 2008 Share Posted December 31, 2008 Is it the same class, or a different class with the same name? if it's the same class Regardless, don't include/require the class, but rather declare the __autoload() function on the first page so that it requires it only when necessary. __autoload Read their warnings about verifying class names/etc so it doesn't try to include just anything it wants. if not.. ? Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-727272 Share on other sites More sharing options...
drath Posted December 31, 2008 Author Share Posted December 31, 2008 I'll take a look at __autoload() but in the meantime I will tell you that it's the same class, different files, with different values (so essentially a different class with the same class name). Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-727274 Share on other sites More sharing options...
xtopolis Posted December 31, 2008 Share Posted December 31, 2008 If it's a class of the same name, but different content, I'm not sure how you can solve that. Not until PHP6 will we have namespaces..... and you're saying you are unable to rename either class.. why? Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-727276 Share on other sites More sharing options...
Mark Baker Posted December 31, 2008 Share Posted December 31, 2008 I know APD allows functions to be renamed dynamically. Don't know if the same can be applied to classes, renaming each class after you've finished whatever processing you do with each, before including the next. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-727277 Share on other sites More sharing options...
drath Posted January 1, 2009 Author Share Posted January 1, 2009 I can't rename the classes because they are part of a content management system, the checking utility I am making is for a server with (in some cases) hundreds of CMS on a single server. Here's a couple brainstorms: 1. Opening the file (using fopen or whatever) and parsing out the variables I need. Obviously this solution is very inefficient especially for a loop that will run hundreds of times on hundreds of files. It will also be a huge hassle and amount of coding. It could be alright though since I only need to get variable values. 2. Somehow loading the class, then unloading the class, or even unloading the include/require. I doubt this is possible. 3. Somehow include the .php files, but don't load the classes/functions, rather... let them be called from the checker script. I also doubt this is possible. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-727306 Share on other sites More sharing options...
drath Posted January 2, 2009 Author Share Posted January 2, 2009 Funnily enough, I found this: http://us3.php.net/runkit UNFORTUNATELY, it doesn't rename/delete classes, only functions... Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728131 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 Call me crazy, but I think I'm entirely missing your goal here. What EXACTLY are you doing with your "site checker"? How is the site class being included multiple times? Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728132 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 Like, DarkWater, I'm not really sure what you are trying to do here. But, consider executing the PHP files using exec() or shell_exec() instead of requiring them. This may be useless depending on what you are doing, but maybe not Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728134 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 If you "have to have" different classes (codewise) under one name... there's something seriously wrong with your application design... Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728138 Share on other sites More sharing options...
drath Posted January 2, 2009 Author Share Posted January 2, 2009 Sorry, I will provide more information, note that I am not really a PHP developer/programmer, but I have intermediate skill/knowledge working with it which is why I come here for the "freak/expert" consultation. One our web server(s) we sometimes host up to 100 different sites, most of which are content management based. Each one of these CMS-based websites have a version.php and a config.php. Most CMS use classes inside both of these files so that the CMS can call and get the data which is smart; however, it becomes a problem when I want to grab this information on multiple sites/directories - you cannot load the same class/function twice. The purpose of the application is to get things like the version, folder permissions, other config information, etc in one big batch instead of going through each of the site manually (which can take hours/days). Not only will this help keep our versions up to date, keep our configs clean, but also help us spot config errors, security issues (permissions), and other problems depending how complex I make this. I could also do rudimentary database checks. For the company I work for this has become a big problem and this would be the best solution - unfortunately I am probably going to have to use fopen/curl to parse out the information I need; unfortunately still, I really suck with regex and the like. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728159 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 So the config data is stored in a 'site' class in each CMS? I'd suggest making a small rewrite to the class and have it load all the data from a configuration (.ini) file. The change will be transparent to the user (they won't notice a difference), and it would be much easier to read in. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728161 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 This is interesting... You should certainly not include those files, as you don't want to actually run them. Try file_get_contents for example and then parse the files. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728163 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 I think you should write a separate php script to the load the classes and return useful information. Then, from a parent script, you can call the separate script via shell_exec(). That way, you don't have to load classes twice. parent script (psuedocode) foreach site ressult = shell_exec('child_script.php path/to/site/config/file'); if result == true do something else do something else child script (pseudocode) path_to_config = script_argument_1 require(path_to_config); // do whatever you need to do return true on success false on failure Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728169 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 @flyhoney: I don't know if executing the script is that good. It'll take kind of long and potentially have side effects. @Mchl: That idea works too. Parsing it could be annoying though. xD Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728172 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 I think that if feasible, then moving all those variables to separate ini files is actually the best idea of those presented. Parsing these files is really easy considering there are ready tools for that. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728176 Share on other sites More sharing options...
drath Posted January 2, 2009 Author Share Posted January 2, 2009 @DarkWater I did think of this but in the end I decided against this. I could modify the class, or even change the file (to not use a class for example); however, this will cause multiple problems since the CMS was designed to use that class in numerous (probably hundreds) of different places - I would have to make changes to all those files. The secondary reason is because when the CMS gets updated... all those changes go bye bye - it's self defeating. @Mchl This is what I am currently looking into; however, I'm lost on the regex thing currently. Basically all I need from the files is everything that is a variable, and then put them into their own variables. Programmatically I would take everything from "$", look for the value after " = " and end at ";" then place that value into a variable that would be the name after the "$". Structurally it's fairly simple, I just have no idea what I am doing, haha. @flyhoney This is an interesting approach. I might have to look into this as well. This looks easier for me to do than reading/parsing file contents (just based on my skill level). Thanks for the help so far guys. I think it is kind of weird that there would be on unset/reset for classes/functions/objects in PHP - but it's probably to reduce bad programming practice. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728180 Share on other sites More sharing options...
drath Posted January 2, 2009 Author Share Posted January 2, 2009 Looks like I am back to square one... or whatever. Supposedly file_get_contents, is not meant to parse php files and will not get all the data needed. As per this comment: Quote In response to EOD (07-Aug-2008 08:34) - this function is not designed to parse php files. It only returns the full contents of any file into a string. If you're looking to parse a php file and output the full results into a string, you could use the ob_ functions wrapped around an 'include' instead: <?php ob_start(); include($filename); $return_str = ob_get_contents(); ob_end_clean(); ?> Unfortunately since this solution uses an include... I obviously can't do this. Also, using an eval() will not work either since eval actually loads the class. The execution solution isn't working either because it doesn't look like I can execute .php files directly using exec/shell_exec. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728230 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 Quote The execution solution isn't working either because it doesn't look like I can execute .php files directly using exec/shell_exec. Make sure you are setting up your command properly: <?php shell_exec('php path/to/script.php param1 param2 etc...'); ?> This is assuming that the php binary is in your path etc.. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728234 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 You need to call php interpreter to execute a .php file So your shell command would look like /usr/bin/php /www/pathtosite/classfile.php What I meant with using file_get_contents was to parse the class file as if it was a text file. I got an idea though. You could 1. Load the file with class into a string using file_get_contents 2. Rename class using str_replace or other string functions into something like class123 3. Save this file in your directory with temporary name 4. include this temporary file to run it Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728236 Share on other sites More sharing options...
drath Posted January 2, 2009 Author Share Posted January 2, 2009 Still not working, I have the following (although i've tried many combination): echo $result = shell_exec('/usr/bin/php /data/www/versionchecker/loader.php'); in the loader.php I have: print "TEST"; echo "TEST"; As per your second suggestion... I would probably be against creating separate files each time it is run just because efficiency and the like. But it might have to do if the exec thing refuses to work . Basically what I would do is copy the version.php contents (or whatever file), rename it to (variablename).php, replace the class in it to (variablename).php and load that one instead within the loop. It could work. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728243 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 Quote Still not working, I have the following (although i've tried many combination): echo $result = shell_exec('/usr/bin/php /data/www/versionchecker/loader.php'); in the loader.php I have: print "TEST"; echo "TEST"; As per your second suggestion... I would probably be against creating separate files each time it is run just because efficiency and the like. But it might have to do if the exec thing refuses to work . Basically what I would do is copy the version.php contents (or whatever file), rename it to (variablename).php, replace the class in it to (variablename).php and load that one instead within the loop. It could work. You are going to need to figure out the correct paths to everything. Try the command 'php -v'. That should dump a bunch of version information. If that works, than you know that php is in your path. Next make sure you have the path to the script you are trying to run correct. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728246 Share on other sites More sharing options...
drath Posted January 2, 2009 Author Share Posted January 2, 2009 I did try getting the correct paths for them before replying, for php: both "php" and "/usr/bin/php" display for getting -v. Therefore, I am assuming there is something wrong with the loader.php path even though it's physically at "/data/www/versionchecker/loader.php" on the web server, so I am not sure. I also tried "/versionchecker/loader.php", "loader.php", and "../../data/www/versionchecker/loader.php" thinking it might be relative... but nothing is working. Maybe some kind of setting is turned off... I do have all errors/reporting on though so something should have displayed in that case. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728261 Share on other sites More sharing options...
flyhoney Posted January 2, 2009 Share Posted January 2, 2009 Try running this command: php -r 'echo getcwd() . "\n";' And tell us what output you get. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728276 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 Okay. Can you show me an example of the file that you want to parse? I can probably work out a parsing routine pretty easily with file_get_contents() and preg_match(). Give me some example data. Quote Link to comment https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/#findComment-728291 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.