TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   __get and __set ?? (http://www.talkphp.com/advanced-php-programming/1489-__get-__set.html)

Tanax 11-17-2007 08:03 PM

__get and __set ??
 
Hi.

Can someone please explain how they work?

PHP Code:

private $tables = array();
public function 
__set($field$value) {

$this->tables[$field] = $value;

}

public function 
__get($field) {

return 
$this->tables[$field];



PHP Code:

$object->user_name 'user_name';
$object->user_reg_date 'register_date'

That would set tables['user_name'] to user_name, and tables['user_reg_date'] to register_date.

However, how do I GET them??

PHP Code:

echo $object->user_reg_date

???

Nor 11-17-2007 09:06 PM

http://phpdiscovery.com/php-5-behave...-a-magic-wand/ Check that article, quite helpful.

Tanax 11-17-2007 09:25 PM

Quote:

Originally Posted by Nor (Post 4337)

Thanks! But I already know WHAT it does. I just wanna know HOW to do it ;)
But thanks :)

Wildhoney 11-18-2007 06:08 PM

Easy.

php Code:
echo $object->user_reg_date;

Tanax 11-18-2007 06:28 PM

Awesome :D

Thanks!


EDIT: How did you do that php box? :O

Wildhoney 11-18-2007 07:18 PM

Quote:

Originally Posted by Tanax (Post 4377)
Awesome :D

Thanks!


EDIT: How did you do that php box? :O

http://www.talkphp.com/showthread.php?t=1406 :)

[highlight=php] var myVar = myFunc(myArg); [/highlight]

Tanax 11-18-2007 09:07 PM

Awesome :D:D
Thanks ;)

maZtah 11-19-2007 07:07 AM

Here's another very useful 'tutorial' to learn the Magic functions!

http://hudzilla.org/phpwiki/index.ph...agic_functions

Haris 11-19-2007 01:43 PM

I don't see a point in using set. How can you set variables which doesn't exists in the class?

Tanax 11-19-2007 03:52 PM

Lol, what??

I'm not setting a variable..

Wildhoney 11-20-2007 12:28 AM

Quote:

Originally Posted by Haris (Post 4410)
I don't see a point in using set. How can you set variables which doesn't exists in the class?

Magic methods, Haris. Crazy, but it's what PHP have gone for. Makes things like this easier if you wish to simply set and get many variables.

Tanax 11-20-2007 07:38 AM

But I'm not setting a variable? =//

I'm just assigning a value to a key in the array tables...


All times are GMT. The time now is 04:31 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0