TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Return self ? (http://www.talkphp.com/advanced-php-programming/3597-return-self.html)

Tanax 11-12-2008 07:28 PM

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

??

Runar 11-12-2008 08:06 PM

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.

Salathe 11-12-2008 09:03 PM

As far as I'm aware, that's not possible at the moment.

Tanax 11-12-2008 10:57 PM

Quote:

Originally Posted by Runar (Post 19571)
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 (Post 19572)
As far as I'm aware, that's not possible at the moment.

Doh! Okey, thanks!

jlodell 02-22-2009 07:37 AM

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

Tanax 02-27-2009 11:21 PM

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

jlodell 02-28-2009 06:57 AM

@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.

Tanax 02-28-2009 12:46 PM

Sure np ! I didn't think you were flaming though, I just wanted to make my point more clear if someone didn't understand ^^

frome 01-18-2013 07:43 AM

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

wangna 01-26-2013 07:59 AM

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



__________________________
Abercrombie Sverige
Lancel Soldes
Nike Free Günstig


All times are GMT. The time now is 03:28 PM.

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