Pages (2):    1 2
Member
Member
jak   30-01-2010, 22:21
#21

The problem is that if you use require_once inside a function variables that are declared in global scope are no longer global.
Example: In your zp-config.php in one of the last lines you find: $_zp_conf_vars = $conf; this assumes that this line is on global scope. When you call require_once inside a function this is no longer true, so $_zp_conf_vars is only a local variable.
To summarize:
PROBLEM: "globals are no longer global"
FIX: Add the line global $_zp_conf_vars; to the beginning of zp-config.php.

You will have to apply similar fixes to other files:


zp-core/lib-GD.php


global $_lib_GD_info;


zp-core/functions-basic.php


global $_zp_conf_vars, $_zp_error, $_zp_imagick_present, $_zp_imagick_present,
$_zp_supported_images, $_zp_options, $_zp_album_folder;


zp-core/functions.php


global $_zp_plugin_scripts, $_zp_loaded_plugins, $_zp_flash_player,
$_zp_HTML_cache, $_zp_themeroot, $_zp_plugin_scripts, $_zp_loaded_plugins,
$_zp_captcha, $_zp_setupCurrentLocale_result, $_zp_exifvars, $_zp_unique_tags,
$_zp_count_tags, $_zp_not_viewable_album_list;


zp-core/template-functions.php


global $_zp_conf_vars, $_zp_gallery;


zp-core/lib-utf8.php


global $_zp_UTF8;
The task to hunt down all other occurences of the same problem is left to the interested reader. Good candidates are the zp-core/lib-* files.

HTH
Jak

Member
Member
sbillard   30-01-2010, 22:33
#22

There is really no cause to place the require_once within the function. So why go throught all this work when you will probably overlook something and things will not work anyway. Just place the require_once in the script that contains the function rather than within it.

Member
Member
jak   30-01-2010, 23:03
#23

This is indeed the best workaround. I think it might be wort to mention it on this page: http://www.zenphoto.org/2009/12/zenphoto-as-a-plug-in-using-zenphoto-functions-from-outside-zenphoto/
At the moment that page only says that it does not work.

BTW: I don't want to sound like a smartass, but the problem are not the function definitions. The problem is the assumption that variables in a .php file that are not inside a function are on the global scope. Normally that is the case but they are not as soon as require_once (or require) is used inside a function. In that case the variables are on the local scope.

I am sure sbillard knows this, but many people do not. It was definitely not obvious to the person that wrote the article I mentioned above.

An Example:
`
includeme.php:


testcase.php

`
doInclude() will result in '->

Member
Member
sbillard   30-01-2010, 23:14
#24

Are you saying that the Wordpress problem is that the require_once calls are within a function call? (I know nothing of Wordpress.)

Member
Member
jak   30-01-2010, 23:30
#25

I did not try it using wordpress - but it sure looks that way.
The first poster mentioned the error "MySQL Error: Zenphoto could not connect to the database server. Check your zp-config.php ..." and said that zenphoto works fine on its own. I got the same behaviour while I tried integrating it into a CMS.

The cause for this is that zp-config.php assumes that $_zp_conf_vars is global. If you use require_once inside a function, the $_zp_conf_vars in zp-config is a local variable (or something like that - it definitely is no longer global) and the data saved to it can not be accessed via the global variable of the same name. Ths is tried inside functions-db.php --> no DB connection is possible.

Member
Member
sbillard   31-01-2010, 02:22
#26

I suppose that could have been something WP changed recently--the plugin stuff used to work. But PHP should also be throwing errors for the undefined $_zp_conf_vars. Maybe WP also supresses those?

Member
Member
sbillard   31-01-2010, 02:23
#27

I suppose that could have been something WP changed recently--the plugin stuff used to work. But PHP should also be throwing errors for the undefined $_zp_conf_vars. Maybe WP also supresses those?

I have little hope that anyone could keep up with the Zenphoto globals well enough to make it so that the files can be required from within a procedure. I hope that there is a different solution available for Wordpress.

Administrator
Administrator
acrylian   31-01-2010, 11:17
#28

The plugin way indeed worked reportley with older Wordpress installs for quite some time. I think it started with WP 2.8 that it stopped. We did not have the time/resource to investigate as we concentrate on Zenphoto as standalone script. But thanks for doing that. This indeed gives a hint why it does not work anymore. As sbillard said there is probably no convenient way to workaround this.

Member
Member
jak   31-01-2010, 12:39
#29

I think I found the relevant part in the wordpress source:
wp-includes/theme.php 990 function load_template($_template_file) { 991 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID; 992 993 if ( is_array($wp_query->query_vars) ) 994 extract($wp_query->query_vars, EXTR_SKIP); 995 996 require_once($_template_file); 997 }
This function will load your template/theme. Therefore even if you use require() on the top level of your template you already are inside a function - you don't have any choice. In that case you probably would have to put the require_once code somewhere else.

I would suggest putting the require_once('path/to/zenphoto/zp-core/template-functions.php') inside wp-config - it seems like this file is included directly (not form inside a function).

I still think that it might be worth it to at least think about a long term solution:
1.) Put as much of the initialization code as you can inside functions (this reduces the occurences of global variables on the "top level"=outside functions).
2.) Not relying on variables to be global unless you declare them as such - i.e. adding global $varName to the top of every file that uses globals on the "top level".
I think that this only affects a handful of files (my guess are zp-core/lib-*.php, zp-core/functions-basic.php, zp-core/functions.php, zp-core/template-functions.php as well as zp-data/zp-config.php).

HTH
Jak

Member
Member
sbillard   31-01-2010, 20:50
#30

I am afraid that there are quite a large number of variables that are required to be global. As I said, the probablity of getting this change right is pretty small. Besides which, it is not actually a zenphoto issue. Zenphoto has been designed to be a stand-alone system. It is possible to use parts (but certainly not all) of it from other systems, but only if you properly set up its environment.

Member
Member
jak   31-01-2010, 21:19
#31

I completely understand that you focus on Zenphoto as standalone.

I just want to note that global variables are not a problem, the problem occurs only when the global variable is accessed without declaring it to be global. Note that in the following example the acces to $otherVar works as expected.
`
includeme.php

includetest.php

Output:
foo:->otherVal

Member
Member
sbillard   31-01-2010, 22:27
#32

I understand. However there is no requirement to declare a varaible global when it is used outside of a function. It would perhaps be nice if PHP had an option to require this, but it does not so far as I know.

Thus, when a script sets up a variable it normally will not also declare it globally. If you take a look at the scripts you will see that there are quite a few such variables.

Again, I reiterate. The probability of success in this venture is small. The effort is large. And the result is not directly applicable to Zenphoto.

Anyone wishing to instantiate Zenphoto from within a function is welcome to create a script that declares these variables and include it with the Zenphoto ones. This is not an effor that has any priority with me.

Pages (2):    1 2
  
Powered By MyBB, © 2002-2026 MyBB Group.
Made with by Curves UI.