Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008, 04:42 AM
WebProWorld New Member
 

Join Date: Mar 2008
Posts: 3
Callme RepRank 0
Default Function to load vars in PHP?

I made an app in PHP, where a large string of vars is loaded (and can be reloaded in certain instances to validate them).
Basically, I want to have a function where vars are loaded, but I don't want them to be global, because they need to stay in the validation functions.
So, I know this won't work, but this is what I want to do:
INSTEAD of this:
Code:
$var1 = $systemvars['var1']
$var2 = $systemvars['var2']
ect.....I WANT to do this:
Code:
function load_vars()
{
$var1 = $systemvars['var1']
$var2 = $systemvars['var2']
ect.....
}
function valid()
{
load_vars);
if (isset($var1 ))
echo "hi!";
}Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-24-2008, 01:27 AM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Posts: 328
Easywebdev RepRank 1
Default Re: Function to load vars in PHP?

You are beginning to get into enterprise application design.
You realise that "Global" variables are bad design and should be avoided.

So what to do?

There are two schools of thought as to how to handle global variables.

1) Store all global variables in an object that holds an array of objects and an array of variables. (a registry)

You can then pass this object to each function that needs its (dependency injection) or you can create a global registry.

The registry should contain all your applications global variables and be passed from one object to another.

Don't be tempted to create the registry as a singelton otherwise you might as well use $_global['key']=$somevar.

2) use the global super array for what it was intended to do. $_global['my_var'] = $my_var.

The java/php oop purists will tell you that global variables are evil, and they are, but there are multiple ways of passing a single object that "contain" these "evil" variables. The bottom line is to use what you feel comfortable using.
__________________
"I have not failed. I have found 10,000 ways that don't work" - Thomas Edison.
"The secret to creativity is knowing how to hide your sources" - Albert Einstein.

Last edited by Easywebdev : 03-24-2008 at 02:00 AM.
Reply With Quote
  #3 (permalink)  
Old 04-21-2008, 07:57 PM
WebProWorld New Member
 

Join Date: May 2006
Posts: 12
Chiefos RepRank 0
Default Re: Function to load vars in PHP?

Callme I think what your after is a means of setting a buch of variables and having methods appropriate to these vars without having the variables declared gloabally (Note that when I say globally I dont mean in the $_globals array but globally within the variable scope of the page).

Basically what I think you need to look at is a little basic OOP, here you can declare properties which are only avaliable in the scope of that object instance so in your case:

N.B. Its been a while since ive done php so syntax may be wrong:

<?php

class SomeClassWithVariablesToValidate
{
$Var, $Varb, $Varc;

public __Construct()
{
$this->ResetVariables();
}
public ResetVariables()
{
$this->Var = 0;
$this->Varb = 1;
$this->Varc = 2;
}
public Validate()
{
if($this->Var!=null && this->Varb!=null && this->Varc!=null )
return true;

return false;
}
}


//Test usage
$pointlessClass = new SomeClassWithVariablesToValidate();

if($pointlessClass->Validate())
echo "Validated";
?>
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Variables outside a function ackerley1 Web Programming Discussion Forum 4 10-30-2007 07:30 PM
IBM Paves Express Track For VARs DougC Insider Reports 0 08-29-2006 11:28 AM
Form and Function grease Graphics & Design Discussion Forum 0 09-19-2005 12:36 AM
Why has this function used in that code? abalfazl Web Programming Discussion Forum 2 03-11-2005 02:24 PM
MySQL/PHP Combining 3 vars into 1 bodgekaloopie Database Discussion Forum 15 09-08-2004 05:54 PM


Search Engine Optimization by vBSEO 3.2.0