TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-17-2007, 08:03 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default __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
???
Tanax is offline  
Reply With Quote
Old 11-17-2007, 09:06 PM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

http://phpdiscovery.com/php-5-behave...-a-magic-wand/ Check that article, quite helpful.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 11-17-2007, 09:25 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Nor View Post
Thanks! But I already know WHAT it does. I just wanna know HOW to do it ;)
But thanks :)
Tanax is offline  
Reply With Quote
Old 11-18-2007, 06:08 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Easy.

php Code:
echo $object->user_reg_date;
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-18-2007, 06:28 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Awesome :D

Thanks!


EDIT: How did you do that php box? :O
Tanax is offline  
Reply With Quote
Old 11-18-2007, 07:18 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Awesome :D

Thanks!


EDIT: How did you do that php box? :O
Added Geshi Syntax Highlighting :)

[highlight=php] var myVar = myFunc(myArg); [/highlight]
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-18-2007, 09:07 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Awesome :D:D
Thanks ;)
Tanax is offline  
Reply With Quote
Old 11-19-2007, 07:07 AM   #8 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

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

http://hudzilla.org/phpwiki/index.ph...agic_functions
maZtah is offline  
Reply With Quote
Old 11-19-2007, 01:43 PM   #9 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

I don't see a point in using set. How can you set variables which doesn't exists in the class?
Haris is offline  
Reply With Quote
Old 11-19-2007, 03:52 PM   #10 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Lol, what??

I'm not setting a variable..
Tanax is offline  
Reply With Quote
Old 11-20-2007, 12:28 AM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by Haris View Post
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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-20-2007, 07:38 AM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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

I'm just assigning a value to a key in the array tables...
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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


All times are GMT. The time now is 02:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design