get_defined_vars doesn't return superglobals when...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:The function works perfectly fine for my needs. .. not that I ever use it.
Oh no, it works fine for me too... But yet, after trying arborint's example, it seems that the function doesn't work as it should work according to the manual, am I right in that part?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As far as I see, the manual describes exactly what it does, correctly. I can understand the function's name being slightly confusing though. You could always alias it to get_scope_vars() maybe, or just accept that only in the global scope will you see the superglobals and move on.

:)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I don't see anything wrong with the manual either.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:or just accept that only in the global scope will you see the superglobals and move on.

:)
That's totally fine. Actually, that's exactly what I needed - I don't need the superglobals :P

I was working on a settings class and that's why I used get_defined_vars.
I have a normal settings file like this one:

Code: Select all

<?php

	$some_config_value = 'whatever';
	$some_config_value2 = 'whatever2';
Do you know of any better way to read these values into an array where the variable names would be the keys and the variable contents would be the values?

Thanks :wink:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

feyd wrote:There's a distinction in programming languages between "declared" and "defined" however. I would probably prefer "instantiated." Although I can't expect most people to spell that one right. :D
I believe that "var $myvar;" is considered a declaration and "var $myvar = 1;" is considered a definintion, when the two terms are distinguished -- mostly the mean the same thing.
Oren wrote:Oh no, it works fine for me too... But yet, after trying arborint's example, it seems that the function doesn't work as it should work according to the manual, am I right in that part?
It seem to work accoring to the manual to me, it is just not completely consistent for practical reasons -- external and internal. Many functions works as they should, though not completely consistently, and thank goodness for that.
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Oren wrote:

Code: Select all

<?php

	$some_config_value = 'whatever';
	$some_config_value2 = 'whatever2';
Do you know of any better way to read these values into an array where the variable names would be the keys and the variable contents would be the values?
When doing configurations with php files, I usually put all the variables into an array to begin with. Similar to how phpBB stores the language strings in $lang. There are other instances where I've stored them in an value object.
arborint wrote:I believe that "var $myvar;" is considered a declaration and "var $myvar = 1;" is considered a definintion, when the two terms are distinguished -- mostly the mean the same thing.
Correct on both counts. .. not that I was expecting you to be wrong. Image
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

That's nice and cool, but I want it to be as simple as possible for people who don't know PHP to fill it out. I may consider using an array though, thanks for the suggestion.
Any issues you can think of with my way - using get_defined_vars() ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Oren wrote:That's nice and cool, but I want it to be as simple as possible for people who don't know PHP to fill it out. I may consider using an array though, thanks for the suggestion.
Any issues you can think of with my way - using get_defined_vars() ?
If you need to allow people to hand edit the config and not know PHP, I wouldn't use a PHP script for the configuration then. Likely, it would be an INI, or not give them direct editing control at all.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Ok, I'll put more thought into it once I write a real application. Right now, I'm just doing this to improve my OOP knowledge and working with patterns.

Any good OOP books (PHP5) by the way? New books please, not books from 2004 even if they are great.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you want to improve your OOP knowledge, I'd use a Value Pattern, or similar.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

feyd wrote:If you want to improve your OOP knowledge, I'd use a Value Pattern, or similar.
I'd like to master the basics first :P If you remember from one of my posts, I mentioned that I learned OOP from a tutorial on the Zend site... Now I want to learn it from the beginning as it should be, and not from online tutorials.
I know it's kinda strange, but even though I'm an OOP noob, I still learn about advanced things like patterns. Advanced relative to my OOP knowledge :P
Post Reply