Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/139052-redeclaring-class-dilemma/
Share on other sites

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.. ?

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.

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.

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.

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?

@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.

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

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..

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

 

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.