Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Getting var name calling the method...


[SOLVED] Getting var name calling the method...

Reply
  #1 (permalink)  
Old 05-15-08, 03:56 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Spin [SOLVED] Getting var name calling the method...

Hi,

First here is a example script:
PHP Code:

<?php


class Test
{
    public function 
__construct()
    {
        
$name 'my_var' ;
        
$this->$name =& $this ;
    }

    public function 
foo()
    {
        
// ????
    
}
}

$test = new Test ;
$test->my_var->foo() ;

?>
First the constructor creates a variable with reference to it self.
Now when we create a instance of the class and use $test->my_var->foo() ;
how can i find out who called it... foo() would return "my_var"

Now i know your thinking, whats the point in that...
Well these variable will be db table names and this is for looks really:

$this->db->table_name->action() ;

Its for a framework iv been designing,
Any ideas?

Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #2 (permalink)  
Old 05-15-08, 04:41 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Perhaps you're looking for __get()

PHP Code:

<?php


class Test
{
    private 
$table_name;
    
    
    public function 
__construct()
    {
    }

    public function 
foo()
    {
        echo 
$this->table_name;
    }
    
    public function 
__get($table_name)
    {
        
$this->table_name $table_name;
        return 
$this;
    }
}

$test = new Test ;
$test->my_var->foo() ;

?>
I'd probably do it this way, though.
PHP Code:

class Test

{
    private 
$table_name;
    
    public function 
foo()
    {
        echo 
$this->table_name;
    }
    
    public function 
table($table_name)
    {
        
$this->table_name $table_name;
        return 
$this;
    }
}

$test = new Test ;
$test->table('bar')->foo() ; 
Reply With Quote
  #3 (permalink)  
Old 05-15-08, 05:40 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
OMG the second example! I didnt know you could do that!
Wow youve made my day

Thanks again Nico,
Lex
__________________
01010000 01001000 01010000
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
RSS Using RSS2Html Script VKX PHP 1 10-16-06 05:27 PM
Paypal Generator Hunter JavaScript 1 08-06-06 07:49 AM
link to iframe//help moose JavaScript 1 01-29-05 12:26 PM
Help with dynamic select in dynamic table noviceforever JavaScript 1 06-23-04 01:20 PM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM


All times are GMT -5. The time now is 07:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.