Page 1 of 1

Simple question about Class'es and vars.

Posted: Sat Dec 06, 2008 4:25 am
by guygk

Code: Select all

<?php
class test {
    public $some_var;
}
 
$data['some_var'] = 'test'; 
print_r(new test($data));
// I need this way to set $some_var to 'test'
?>
I don't see how to make this work.

Re: Simple question about Class'es and vars.

Posted: Sat Dec 06, 2008 4:28 am
by guygk
Seems like this is the only way?

Code: Select all

<?php
class test {
    public $some_var;
}
 
$data = new test($data);
$data->some_var = 'test';
print_r($data);
// I need this way to set $some_var to 'test'
?>
But the problem is that I need to load those vars from array. There is no way to do this without writing custom handler?

Re: Simple question about Class'es and vars.

Posted: Sat Dec 06, 2008 4:37 am
by requinix
There is a way without a "custom handler" but it's probably a stupid idea.

Code: Select all

class test {
    public $some_var;
 
    public function __construct($array) {
        if (isset($array["some_var"])) $this->some_var = $array["some_var"];
    }
}
 
$data = array("some_var" => "test");
print_r(new test($data));

Re: Simple question about Class'es and vars.

Posted: Sat Dec 06, 2008 4:48 am
by guygk
So basically that is what I meant by "custom handler".

Re: Simple question about Class'es and vars.

Posted: Sat Dec 06, 2008 4:56 am
by requinix
guygk wrote:So basically that is what I meant by "custom handler".
Well then, define what you consider to be a custom handler. You're going to have to write code: what type and style is up to you.

And please consider whether you want something because it's better or because it's easier.