Simple question about Class'es and vars.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guygk
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 4:06 pm

Simple question about Class'es and vars.

Post 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.
guygk
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 4:06 pm

Re: Simple question about Class'es and vars.

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple question about Class'es and vars.

Post 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));
guygk
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 4:06 pm

Re: Simple question about Class'es and vars.

Post by guygk »

So basically that is what I meant by "custom handler".
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple question about Class'es and vars.

Post 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.
Post Reply