Accessing Constants through include() - not finding them

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Accessing Constants through include() - not finding them

Post by orbstra »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I keep gettin an error when I use my function (code below), like this:

I am trying to make a  function that returns the value of a requested constant. The constant is found in an external file, the file just has a bunch of constants defined in it (it is the options file for my app)

Code: Select all

//Used like:
include(get::dir('theme') . '/' . get::opt('TP01') . '/head.php');

class get
{
      //Function found later in the script (simplified)
      function opt($num)
      {
	        require(get::dir('config') . 'config.php');
        	$return = constant($num);
	        global $return;
	        return $return;
       }
}
this is the error I get:

Code: Select all

Warning: constant() [function.constant]: Couldn't find constant TP01 in C:\Documents and Settings\Administrator\My Documents\My Projects\Web Projects\Guava\guav-includes\guav_library.php on line 224
TP01 is clearly defined in config.php as

Code: Select all

//The theme you would like to use (guav-template/)
DEFINE("TP01", "default");
thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Maybe you should use defined() too. What debugging have you performed?
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

my IDE hasn't given me an error, and I have gon between both the include() and require() methods of getting the external file full of all the constants, no luck. I also tried directly linking to the config.php file, only to find that my constants cannot be found:

Code: Select all

include('guav-config/' . 'config.php');
I also tried placing

Code: Select all

$return = constant($num);
inside the external file only to receive a boat-load of more errors.

PS: I have looked very far and wide for an answer to this problem, only to find irrelivant answers and old useless forum topics pertaining to pre-written PHP frameworks and CMS systems... I have done my research and a fair share of tinkering with the script, only to find I am still completely lost.
Last edited by orbstra on Tue Jan 02, 2007 11:59 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not really concerned with what your IDE thinks, but what your code has that's helping debug. getcwd() may be helpful. So may is_readable().

What additional errors did you get when you added the constant() call to the included file?
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

sorry I mis-wrote my previous statement, I get the same errors when I move the constant() in the external file.

I also have found that the link to the external file is OK - I put an echo statement in the external file

is_readable returned true
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you run phpinfo(), do you find define() in the disabled functions list? If not, does running get_defined_constants() shed any light?
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

OMG IT WAS THE SIMPLEST THING!!! I needed to get rid of

Code: Select all

global $return
in the function.. That was the last thing I would think of, it seems so harmless yet it ruins applications!

thnx for your help feyd
PS: stumbled across your blog, hope you are feeling better from that accident!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

orbstra wrote:I needed to get rid of global $return in the function..
Does that seem weird to anyone else?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

ole wrote:
orbstra wrote:I needed to get rid of global $return in the function..
Does that seem weird to anyone else?
No. Basically the global directive does not make a variable global. Global fetchs a variable from the list of variables with scope of the main code branch. That will overwrite any local variable with the same name even if it's not set outside of the local scope.

Code: Select all

<?php

	$a = "bye";

	function test() {
	
		$a = "hello";
		global $a;
		return $a;
	
	}
	
	echo test();
	
?>
That will kill "hello" by getting "bye" from outside the function. Where you use global is very important.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm aware of the usage of global and it doesn't explain this:
Warning: constant() [function.constant]: Couldn't find constant TP01 in C:\Documents and Settings\Administrator\My Documents\My Projects\Web Projects\Guava\guav-includes\guav_library.php on line 224
That is what I think is strange.

But yes I completely see that even with a defined constant opt() wouldn't work because of use of global.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yeah, global doesn't correlate to constant() which is odd.

What accident are you referring to orbstra? The last accident I was involved in was three years ago in which I wasn't injured, but I nearly beat the living daylights out of the guy that hit us.
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

feyd wrote:Yeah, global doesn't correlate to constant() which is odd.

What accident are you referring to orbstra? The last accident I was involved in was three years ago in which I wasn't injured, but I nearly beat the living daylights out of the guy that hit us.
oh mabye it was someone else's blog I stumbled upon, something about being hit by a truck.. ne ways

I guess I had used $return later in the application, so once I got rid of global it worked for a little but now I find myself with my opt() function not working again :-(. I tried to roll back to when it worked but still no luck

I just tried placing all the constants in the external file in a big class, then changed each from define to const, then used this code for my function:

Code: Select all

public function opt($num)
		{
			include(self::dir('config') . 'config.php');		
			return constant(config::$num);
		}
the external class that all the constants are in is named config.. although now my app can't find the config class.. should I roll back to how it was before, or should I stick it out wit this method... eitherway I am not having good luck with this, I just can't seem to get it to work, and I have no clue why
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Code: Select all

// This
$num = 'taco' ;
config::$num ;


// Does not resolve to
config::taco


// Using :: and then $ accesses a static variable like below
class config {
  var $num = 2 ;
  const someOtherVar = 10 ;
}

$num = 'someOtherVar' ;
echo config::$num ; // prints 2
If the config class is not found then I am assuming your include statment is not working correctly. Try manually putting the file path in your include statement to see if it works, if not then get your includes working first.
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

Begby wrote:

Code: Select all

// This
$num = 'taco' ;
config::$num ;


// Does not resolve to
config::taco


// Using :: and then $ accesses a static variable like below
class config {
  var $num = 2 ;
  const someOtherVar = 10 ;
}

$num = 'someOtherVar' ;
echo config::$num ; // prints 2
If the config class is not found then I am assuming your include statment is not working correctly. Try manually putting the file path in your include statement to see if it works, if not then get your includes working first.
my opt function is in a class itself... if that is any help.. but I see what you are saying wtih the :: operator

Code: Select all

public function opt($num)
                {
                        include(self::dir('config') . 'config.php');       
                        return constant($num); // A mis-type
                }
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What do you need opt for anyway orbstra?
Post Reply