PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 8th, 2008, 03:12 PM
tagmanadvance's Avatar
tagmanadvance tagmanadvance is offline
Kage Bunshin
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
Posts: 718 tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 55 m 23 sec
Reputation Power: 184
Send a message via AIM to tagmanadvance Send a message via Yahoo to tagmanadvance Send a message via XFire to tagmanadvance
PHP-General - Dynamically set variables within function-scope?

Is there a variable array similar to the GLOBALS array except within the scope of the current function? I want to dynamically convert an Objects members into variables without having to declare each variable myself. I do not want to use an array. I want to do something like this, except instead of making them global, I would like the variables to be within the scope of the current function.

If that doesn't make sense, I want it to work something like this:
PHP Code:
<?PHP
     
function setObjVars($object) {
          
$members get_object_vars($object);
          foreach(
$members as $key => $member) {
               
$GLOBALS[$key] = $member;
          }
     }

     function 
foozle() {
          
//has object foo
          
$foo = new Object();
          
$foo->"abc";
          
setObjVars($foo);
          print 
"... $d ...";
     }
     
     
foozle();
?>

Except instead of setting globals I want to set them as variables within the scope of the "foozle" function.
__________________
"Java makes impossible things possible, but makes easy things difficult." - Somebody

Reply With Quote
  #2  
Old May 8th, 2008, 03:24 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Beware
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2006
Location: Texas, USA
Posts: 1,435 ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level)ManiacDan User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 53 m 15 sec
Reputation Power: 176
Using a combination of extract and get_object_vars, you can do this:
PHP Code:
function foozle() {
    
$foo = new stdClass();
    
$foo->"abc";
    
extract(get_object_vars($foo));
    print 
"... $d ...";

However, what happens when you have a variable called $foo inside of the object $foo? Are you really confident that NONE of your class member variables are the same as ANY of the variables you'll be using in your PHP script? The closed scope of classes is one of their main benefits.

-Dan
Comments on this post
tagmanadvance agrees: Thanks =)
__________________
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Reply With Quote
  #3  
Old May 8th, 2008, 03:40 PM
tagmanadvance's Avatar
tagmanadvance tagmanadvance is offline
Kage Bunshin
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
Posts: 718 tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 55 m 23 sec
Reputation Power: 184
Send a message via AIM to tagmanadvance Send a message via Yahoo to tagmanadvance Send a message via XFire to tagmanadvance
The object in question is actually a mysql object; the fields being returned are query information. I appreciate the advice and will check all my variable names before implementing this method. Thanks again =)

Last edited by tagmanadvance : May 8th, 2008 at 03:49 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Dynamically set variables within function-scope?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway