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-12-2008, 07:28 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 Return self ?

Hi!

You know when you want to use chains in regular classes, you return $this?

PHP Code:
public function smthng()
{

       
$this->somevar 'smthng';
       return 
$this;


My question regards how you do this when the whole class is static(it's a factory)?

PHP Code:
public static function smthng()
{

       
self::$somevar 'smthng';
       return 
self;


??

Also, if this is possible, how does the actual chaining work?
I mean.. do you use it like:
PHP Code:
FACTORY::smthng()::smthng()::smthng
??
__________________
Tanax is offline  
Reply With Quote
Old 11-12-2008, 08:06 PM   #2 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

I know I used $this in a situation similar to your first example. In your last example, the actual chaining, I did something like this:

PHP Code:
$class->first()->second(); 
I am not sure if there is any significant difference on static and non-static classes when chaining, but I guess other people will answer that question.
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
The Following User Says Thank You to Runar For This Useful Post:
Tanax (11-12-2008)
Old 11-12-2008, 09:03 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

As far as I'm aware, that's not possible at the moment.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tanax (11-12-2008)
Old 11-12-2008, 10:57 PM   #4 (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 Runar View Post
I know I used $this in a situation similar to your first example. In your last example, the actual chaining, I did something like this:

PHP Code:
$class->first()->second(); 
I am not sure if there is any significant difference on static and non-static classes when chaining, but I guess other people will answer that question.

Yes, I know how to do actual chainings with the regular classes, it's just the little problem that this isn't a regular class

Quote:
Originally Posted by Salathe View Post
As far as I'm aware, that's not possible at the moment.
Doh! Okey, thanks!
__________________
Tanax is offline  
Reply With Quote
Old 02-22-2009, 07:37 AM   #5 (permalink)
The Wanderer
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
jlodell is on a distinguished road
Default

I'm interested in why you would try to call the same method over and over like that or was that just kind of for show? Do all the methods have to be static, or could you just implement the static factory method(s) and then normal methods for any others:
ClassName::factory()->meth()->again()\
static chaining wont work because it doesn't use a class instance for reference, it uses a class name
jlodell is offline  
Reply With Quote
Old 02-27-2009, 11:21 PM   #6 (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

I don't wanna call the same method over and over xD I just wanted to get my point out, if it was possible to chain static methods
__________________
Tanax is offline  
Reply With Quote
Old 02-28-2009, 06:57 AM   #7 (permalink)
The Wanderer
 
Join Date: Feb 2009
Posts: 7
Thanks: 0
jlodell is on a distinguished road
Default

@tanax: I understand, and I'm definitely not flaming. Just new to the forum and wanted to post a couple things. Just threw the explanation in for those that might not understand the method chaining process or why static methods cannot be chained. Cheers.
jlodell is offline  
Reply With Quote
Old 02-28-2009, 12:46 PM   #8 (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

Sure np ! I didn't think you were flaming though, I just wanted to make my point more clear if someone didn't understand
__________________
Tanax is offline  
Reply With Quote
Old 01-18-2013, 07:43 AM   #9 (permalink)
The Visitor
 
Join Date: Jan 2013
Posts: 1
Thanks: 0
frome is on a distinguished road
Default

thought this was a interesting topic so i made a experiment.

I make a class with static functions and used singleton pattern.

like so
PHP Code:
<?php
class helloWorld{

    private static 
$instance false;

    static private function 
thisInstance(){
        if(
self::$instance === false){
            
self::$instance = new helloWorld;
        }
        return 
self::$instance;
    }

    static public function 
talk($arg){
        echo 
$arg;
        return 
self::thisInstance();
    }

}
then i used
PHP Code:
helloWorld::talk('Yo yo world.')->talk(' sup y\'all'); 
and i worked like a charm :)

output: Yo yo world sup y'all

hope it helps 6 years later XD
frome is offline  
Reply With Quote
Old 01-26-2013, 07:59 AM   #10 (permalink)
The Wanderer
 
Join Date: Jan 2013
Posts: 5
Thanks: 0
wangna is on a distinguished road
Default

I know I used $this in a situation similar to your first example.



__________________________
Abercrombie Sverige
Lancel Soldes
Nike Free Günstig
wangna 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 11:42 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