Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you dynamically create private variables?

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
0
0
US
This is a question for PHP v5.0.4.

How do you dynamically create private variables without predeclaring them as private in a class?

Code:
final class Dynamic
{
   private $sample1;

   public function __construct ( )
   {
      $array = (
        "sample2",
        "sample3",
        "sample4",
        "sample5",
        "sample6",
        "sample7",
        "sample8",
        "sample9",
        "sample10",
      );
      // Here's just some code that would make sense
      // but not necessarily work.
      foreach ( $array as $new_var )
      {
         private $this->$new_var = "";
      }
   }
}

The reason I want to do this is because I do not know what variables I am going to need to create. [tt]$array[/tt] can be changed to nearly everything.

---------------------------------------
 
how about using an associative array?

Code:
final class Dynamic {
	private $vars;

	public function __construct () {

		$keys = array(
			"sample2",
			"sample3",
			"sample4",
			"sample5",
			"sample6",
			"sample7",
			"sample8",
			"sample9",
			"sample10"
		);

		foreach ($keys as $key) {
			$this->vars[$key] = null;
		}
	}

	function get($varname) {
		$val = null;
		if (array_key_exists($varname, $this->vars)) {
			$val = $this->vars[$varname]; 
		}
		return $val;
	}

	function set($varname, $val) {
		if (array_key_exists($varname, $this->vars)) {
			$this->vars[$varname] = $val;
		}
	}
}

$d = new Dynamic();
$d->set("sample2", "sample 2 data");
echo "<p>sample2 null?: " . is_null($d->get("sample2")) . "</p>";
echo "<p>sample22 null?: " . is_null($d->get("sample22")) . "</p>";

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
or

foreach ( $array as $new_var )
{
private $this->$$new_var = "";
}


;)
 
I have your way in consideration jemminger, thanks. But I still need to do something like my example. hos2, your example is almost the same is mine, it doesn't work. Thanks anyway.

Another other suggestions?

---------------------------------------
 
Can you just use temp as a private variable?
Then from outside class you call with:
$c = new Dynamic();
$c->sample1 = ...


--
Mike FN
&quot;8 out of 10 Owners who Expressed a Preference said Their Cats Preferred Web Programmer.&quot;
 
yep my example is almost the same but differs quite a lot because of the $$ assignment

suppose you have a variable
$test='var1';

then $$test='hallo';

echo $var1;

gives back "hallo"

perhaps is there something wrong with $this, I have never seen that before but I just copied you're code

perhaps
foreach ( $array as $new_var )
{
private $$new_var = "";
}

works better
 
@hos2: i tried both your examples and i got this error on all versions of code that you given, and i even tried it using a single $: Parse error: parse error, unexpected T_PRIVATE

@mikefn: what? sorry but i didn't understand what you said. if i'm guessing correctly it looks like the same example jemminger gave me.

thanks guys, i still need help though!

---------------------------------------
 
perhaps because the variable type private doesn't exist in PHP , if you have some reference in a manual where it's used I would like to see but I can't find it on

hey I don't know all the options php has to offer so when someone comes with a private declaration I guess that you did some research about this private type in php ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top