PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > How can I do this -- method chaining
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How can I do this -- method chaining

Réponse
 
LinkBack Outils de la discussion
Vieux 29/01/2008, 18h53   #1
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How can I do this -- method chaining

Constructors return the object, correct? If so, how can I do this:

class Bob {
private $blah;
_construct( $blah ) {
$this->blah = $blah;
}
public getBlah() {
return $this->blah;
}
}

echo Bob( 'Hello!' )->getBlah();

When I try that, I get the message "Undefined function Bob". I've also tried

echo new Bob( 'Hello!' )->getBlah();
echo (new Bob( 'Hello!' ))->getBlah();

but PHP didn't like either of those at all. Is it just not possible
what I'm trying to do?

I'm using PHP5.2.1

thnx,
Chris
  Réponse avec citation
Vieux 29/01/2008, 19h02   #2
Richard Heyes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Christoph Boget wrote:
> Constructors return the object, correct? If so, how can I do this:
>
> class Bob {
> private $blah;
> _construct( $blah ) {
> $this->blah = $blah;
> }
> public getBlah() {
> return $this->blah;
> }
> }
>
> echo Bob( 'Hello!' )->getBlah();
>
> When I try that, I get the message "Undefined function Bob". I've also tried
>
> echo new Bob( 'Hello!' )->getBlah();
> echo (new Bob( 'Hello!' ))->getBlah();


Bob is a class, not a method. You could try this:

<?php
$obj = new Bob();
$obj->getBlah();
?>

It's not method chaining though.

--
Richard Heyes
http://www.websupportsolutions.co.uk

Knowledge Base and desk software for £299pa hosted for you -
no installation, no maintenance, new features automatic and free
  Réponse avec citation
Vieux 29/01/2008, 19h17   #3
Eric Butera
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 1:53 PM, Christoph Boget <christoph.boget@gmail.com> wrote:
> Constructors return the object, correct? If so, how can I do this:
>
> class Bob {
> private $blah;
> _construct( $blah ) {
> $this->blah = $blah;
> }
> public getBlah() {
> return $this->blah;
> }
> }
>
> echo Bob( 'Hello!' )->getBlah();
>
> When I try that, I get the message "Undefined function Bob". I've also tried
>
> echo new Bob( 'Hello!' )->getBlah();
> echo (new Bob( 'Hello!' ))->getBlah();
>
> but PHP didn't like either of those at all. Is it just not possible
> what I'm trying to do?
>
> I'm using PHP5.2.1
>
> thnx,
> Chris
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


http://www.travisswicegood.com/index...pi_here_i_come
  Réponse avec citation
Vieux 29/01/2008, 19h27   #4
Andrew Ballard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 1:53 PM, Christoph Boget <christoph.boget@gmail.com> wrote:
> Constructors return the object, correct?


Actually, I don't think so. I believe constructors return void, while
the 'new' keyword returns a copy of the object.

Andrew
  Réponse avec citation
Vieux 29/01/2008, 19h37   #5
Paul Scott
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining


On Tue, 2008-01-29 at 14:17 -0500, Eric Butera wrote:

> http://www.travisswicegood.com/index...pi_here_i_come
>



Looks like a repurpose of one of my posts:

http://fsiu.uwc.ac.za/index.php?modu...092_1182404204

--Paul


All Email originating from UWC is covered by disclaimer
http://www.uwc.ac.za/portal/public/p...disclaimer.htm

  Réponse avec citation
Vieux 29/01/2008, 19h38   #6
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 2:27 PM, Andrew Ballard <aballard@gmail.com> wrote:

> On Jan 29, 2008 1:53 PM, Christoph Boget <christoph.boget@gmail.com>
> wrote:
> > Constructors return the object, correct?

>
> Actually, I don't think so. I believe constructors return void, while
> the 'new' keyword returns a copy of the object.



im pretty sure constructors return an object instance:
php > class Test { function __construct() {} }
php > var_dump(new Test());
object(Test)#1 (0) {
}

but anyway, how could you even test that __construct() returned void
and the new keyword returned a copy of the object? new essentially
invokes __construct() and passes along its return value, near as i can tell.

Christoph,
if you dont want to write a function in the global namespace, as suggested
in the article, Eric posted, just add a simple factory method in your class,
eg.
<?php
class Test {
public static function getInstance() {
return new Test();
}

public function doSomething() {
echo __METHOD__ . PHP_EOL;
}
}
Test::getInstance()->doSomething();
?>

-nathan

  Réponse avec citation
Vieux 29/01/2008, 19h41   #7
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 2:37 PM, Paul Scott <pscott@uwc.ac.za> wrote:

> Looks like a repurpose of one of my posts:
>
>
> http://fsiu.uwc.ac.za/index.php?modu...092_1182404204



actually, this is slightly different; here we are talking about being
able to immediately invoke a method off the call to the constructor,
whereas in your post you chain calls after storing the instance in a
variable
in the call to the constructor.

-nathan

  Réponse avec citation
Vieux 29/01/2008, 19h43   #8
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

> On Jan 29, 2008 2:37 PM, Paul Scott <pscott@uwc.ac.za> wrote:
> > Looks like a repurpose of one of my posts:
> >

http://fsiu.uwc.ac.za/index.php?modu...092_1182404204
> actually, this is slightly different; here we are talking about being
> able to immediately invoke a method off the call to the constructor,
> whereas in your post you chain calls after storing the instance in a
> variable in the call to the constructor.


Right, and that's what I was trying to avoid, if possible.

thnx,
Chris


__________________________________________________ __________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
  Réponse avec citation
Vieux 29/01/2008, 19h44   #9
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Christoph Boget schreef:
> Constructors return the object, correct? If so, how can I do this:
>
> class Bob {
> private $blah;
> _construct( $blah ) {
> $this->blah = $blah;
> }
> public getBlah() {
> return $this->blah;
> }
> }
>
> echo Bob( 'Hello!' )->getBlah();
>
> When I try that, I get the message "Undefined function Bob". I've also tried
>
> echo new Bob( 'Hello!' )->getBlah();
> echo (new Bob( 'Hello!' ))->getBlah();
>
> but PHP didn't like either of those at all. Is it just not possible
> what I'm trying to do?



class Foo
{
private $x;
private function __construct($x) { $this->x = $x; }
static function init($x) { return new self($x); }
function double() { $this->x *= 2; return $this; }
function triple() { $this->x *= 3; return $this; }
function output() { echo $this->x, "\n"; }
}

Foo::init(2)->double()->triple()->output();

you can't chain of the constructor as Andrew explained.
you may wish to return object clones to chain with as opposed to
the same object - the example below is fairly bogus but it
mgiht be ful to you (btw run the code to see what it actually
does as opposed to what you think it should do ... hey it caught
me out and I wrote it!):

class Foo2
{
private $x;
private function __construct($x) { $this->x = $x; }
static function init($x) { return new self($x); }
function double() { $this->x *= 2; return clone $this; }
function triple() { $this->x *= 3; return clone $this; }
function output() { echo $this->x, "\n"; }
}

$a = Foo2::init(2);
$b = $a->double()->triple();

$a->output();
$b->output();






>
> I'm using PHP5.2.1
>
> thnx,
> Chris
>

  Réponse avec citation
Vieux 29/01/2008, 20h02   #10
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On 29 Jan 2008, at 19:43, Christoph Boget <jcboget@yahoo.com> wrote:

>> On Jan 29, 2008 2:37 PM, Paul Scott <pscott@uwc.ac.za> wrote:
>>> Looks like a repurpose of one of my posts:
>>>

> http://fsiu.uwc.ac.za/index.php?modu...092_1182404204
>> actually, this is slightly different; here we are talking about being
>> able to immediately invoke a method off the call to the constructor,
>> whereas in your post you chain calls after storing the instance in a
>> variable in the call to the constructor.

>
> Right, and that's what I was trying to avoid, if possible.


Why? What exactly do you think you're saving by not putting the
instance in a variable? I can't think of one good reason to do this.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 29/01/2008, 20h08   #11
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 3:02 PM, Stut <stuttle@gmail.com> wrote:

> Why? What exactly do you think you're saving by not putting the
> instance in a variable? I can't think of one good reason to do this.
>


its an esthetic thing; and besides the simple factory method is an
easy workaround to achieve it.
as the article that, Eric, posted mentioned, other languages have
such support; ie javascript:
function Test() {}
Test.prototype = { doSomething : function() { alert('hello'); } }
new Test().doSomething();

this is along the lines of the whole returnAnArray()['someIndex'] thing,
fortunately in this case, theres a workaround in userspace

-nathan

  Réponse avec citation
Vieux 29/01/2008, 20h26   #12
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Nathan Nobbe schreef:
> On Jan 29, 2008 3:02 PM, Stut <stuttle@gmail.com> wrote:
>
>> Why? What exactly do you think you're saving by not putting the
>> instance in a variable? I can't think of one good reason to do this.
>>

>
> its an esthetic thing; and besides the simple factory method is an
> easy workaround to achieve it.
> as the article that, Eric, posted mentioned, other languages have
> such support; ie javascript:
> function Test() {}
> Test.prototype = { doSomething : function() { alert('hello'); } }


^^ !!!! prototypal not class-based inheritance, orange meet apple.

> new Test().doSomething();


besides which this is a dereferenced call and not method chaining,
if you want method chaining in JS you'll have to do extra work (i.e. use 'return this;')

different strokes or something.

>
> this is along the lines of the whole returnAnArray()['someIndex'] thing,
> fortunately in this case, theres a workaround in userspace
>
> -nathan
>

  Réponse avec citation
Vieux 29/01/2008, 20h34   #13
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 3:26 PM, Jochem Maas <jochem@iamjochem.com> wrote:

> Nathan Nobbe schreef:
> > On Jan 29, 2008 3:02 PM, Stut <stuttle@gmail.com> wrote:
> >
> >> Why? What exactly do you think you're saving by not putting the
> >> instance in a variable? I can't think of one good reason to do this.
> >>

> >
> > its an esthetic thing; and besides the simple factory method is an
> > easy workaround to achieve it.
> > as the article that, Eric, posted mentioned, other languages have
> > such support; ie javascript:
> > function Test() {}
> > Test.prototype = { doSomething : function() { alert('hello'); } }

>
> ^^ !!!! prototypal not class-based inheritance, orange meet apple.
>


i never said it was :P

besides which this is a dereferenced call and not method chaining,
> if you want method chaining in JS you'll have to do extra work (i.e. use
> 'return this;')
> different strokes or something.
>


this example was to illustrate that in other languages, such as javascript,
an object
method can be invoked directly from an instance returned by a call to new
(which is
really what were talking about in this thread). whereas, in php, as weve
seen today,
youll have to implement a workaround.

-nathan

  Réponse avec citation
Vieux 30/01/2008, 00h27   #14
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On 29 Jan 2008, at 20:08, "Nathan Nobbe" <quickshiftin@gmail.com> wrote:

> On Jan 29, 2008 3:02 PM, Stut <stuttle@gmail.com> wrote:
> Why? What exactly do you think you're saving by not putting the
> instance in a variable? I can't think of one good reason to do this.
>
> its an esthetic thing; and besides the simple factory method is an
> easy workaround to achieve it.
> as the article that, Eric, posted mentioned, other languages have
> such support; ie javascript:
> function Test() {}
> Test.prototype = { doSomething : function() { alert('hello'); } }
> new Test().doSomething();
>
> this is along the lines of the whole returnAnArray()['someIndex']
> thing,
> fortunately in this case, theres a workaround in userspace


I don't see how it's any more aesthetically pleasing, but each to
their own I guess.

Personally I'd use a static method in this instance. If you need to
create an instance of the class you can do so in the static method and
that way it will get destroyed when the function is done. Otherwise
the object scope is far larger than it needs to be, which IMHO is an
unnecessary waste of resources and certainly less aesthetic.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 30/01/2008, 00h37   #15
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 29, 2008 7:27 PM, Stut <stuttle@gmail.com> wrote:
> Personally I'd use a static method in this instance.


thats what i recommended.

If you need to create
> an instance of the class you can do so in the static method and that way it
> will get destroyed when the function is done. Otherwise the object scope is
> far larger than it needs to be, which IMHO is an unnecessary waste of
> resources and certainly less aesthetic.


lost you on this part ..
whether you create an instance in client code by calling new or
encapsulate the call
to new in a simple factory method there will still be only one
instance of the class,
and it will still be in scope once the method is finished executing,
because all it does
is return an instance of the class its a member of.
maybe you mean something other than what i posted earlier when you say
static method?

-nathan
  Réponse avec citation
Vieux 30/01/2008, 10h56   #16
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Nathan Nobbe wrote:
> On Jan 29, 2008 7:27 PM, Stut <stuttle@gmail.com> wrote:
>> Personally I'd use a static method in this instance.

>
> thats what i recommended.
>
>> If you need to create
>> an instance of the class you can do so in the static method and that way it
>> will get destroyed when the function is done. Otherwise the object scope is
>> far larger than it needs to be, which IMHO is an unnecessary waste of
>> resources and certainly less aesthetic.

>
> lost you on this part ..
> whether you create an instance in client code by calling new or
> encapsulate the call
> to new in a simple factory method there will still be only one
> instance of the class,
> and it will still be in scope once the method is finished executing,
> because all it does
> is return an instance of the class its a member of.
> maybe you mean something other than what i posted earlier when you say
> static method?


You posted a singleton pattern. That means that from the moment you call
the static method until the end of the script that object exists. That's
probably fine for web-based scripts that don't run for long, but I live
in a world where classes often get used in unexpected ways so I tend to
write code that's efficient without relying on the environment it's
running in to clean it up.

This was your code...

<?php
class Test {
public static function getInstance() {
return new Test();
}

public function doSomething() {
echo __METHOD__ . PHP_EOL;
}
}
Test::getInstance()->doSomething();
?>

This would be my implementation...

<?php
class Test {
public static function doSomething() {
$o = new Test();
$o->_doSomething();
}

protected function _doSomething() {
// I'm assuming this method is fairly complex, and involves
// more than just this method, otherwise there is no point
// in creating an instance of the class, just use a static
// method.
}
}
Test::doSomething();
?>

Of course this is just based on what the OP said they wanted to do. If
there is no reason to create an instance of the object then don't do it.
It's fairly likely that I'd actually just use a static method here, but
it depends on what it's actually doing.

But as I said earlier, each to their own.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 30/01/2008, 11h20   #17
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Stut schreef:
> Nathan Nobbe wrote:
>> On Jan 29, 2008 7:27 PM, Stut <stuttle@gmail.com> wrote:
>>> Personally I'd use a static method in this instance.

>>
>> thats what i recommended.
>>
>>> If you need to create
>>> an instance of the class you can do so in the static method and that
>>> way it
>>> will get destroyed when the function is done. Otherwise the object
>>> scope is
>>> far larger than it needs to be, which IMHO is an unnecessary waste of
>>> resources and certainly less aesthetic.

>>
>> lost you on this part ..
>> whether you create an instance in client code by calling new or
>> encapsulate the call
>> to new in a simple factory method there will still be only one
>> instance of the class,
>> and it will still be in scope once the method is finished executing,
>> because all it does
>> is return an instance of the class its a member of.
>> maybe you mean something other than what i posted earlier when you say
>> static method?

>
> You posted a singleton pattern.


huh? the OPs getInstance() method returns a new object on each call, hardly
a singleton is it?

That means that from the moment you call
> the static method until the end of the script that object exists. That's
> probably fine for web-based scripts that don't run for long, but I live
> in a world where classes often get used in unexpected ways so I tend to
> write code that's efficient without relying on the environment it's
> running in to clean it up.


are you saying that the OPs getInstance() method causes each new instance
to hang around inside memory because php doesn't know that it's no longer referenced,
even when it's used like so:

Test::getInstance()->doSomething();

and that your alternative does allow php to clean up the memory?

>
> This was your code...
>
> <?php
> class Test {
> public static function getInstance() {
> return new Test();
> }
>
> public function doSomething() {
> echo __METHOD__ . PHP_EOL;
> }
> }
> Test::getInstance()->doSomething();
> ?>
>
> This would be my implementation...
>
> <?php
> class Test {
> public static function doSomething() {
> $o = new Test();
> $o->_doSomething();
> }
>
> protected function _doSomething() {
> // I'm assuming this method is fairly complex, and involves
> // more than just this method, otherwise there is no point
> // in creating an instance of the class, just use a static
> // method.
> }
> }
> Test::doSomething();
> ?>
>
> Of course this is just based on what the OP said they wanted to do. If
> there is no reason to create an instance of the object then don't do it.
> It's fairly likely that I'd actually just use a static method here, but
> it depends on what it's actually doing.
>
> But as I said earlier, each to their own.
>
> -Stut
>

  Réponse avec citation
Vieux 30/01/2008, 11h28   #18
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Jochem Maas wrote:
> Stut schreef:
>> Nathan Nobbe wrote:
>>> On Jan 29, 2008 7:27 PM, Stut <stuttle@gmail.com> wrote:
>>>> Personally I'd use a static method in this instance.
>>>
>>> thats what i recommended.
>>>
>>>> If you need to create
>>>> an instance of the class you can do so in the static method and that
>>>> way it
>>>> will get destroyed when the function is done. Otherwise the object
>>>> scope is
>>>> far larger than it needs to be, which IMHO is an unnecessary waste of
>>>> resources and certainly less aesthetic.
>>>
>>> lost you on this part ..
>>> whether you create an instance in client code by calling new or
>>> encapsulate the call
>>> to new in a simple factory method there will still be only one
>>> instance of the class,
>>> and it will still be in scope once the method is finished executing,
>>> because all it does
>>> is return an instance of the class its a member of.
>>> maybe you mean something other than what i posted earlier when you say
>>> static method?

>>
>> You posted a singleton pattern.

>
> huh? the OPs getInstance() method returns a new object on each call, hardly
> a singleton is it?


Quite right too. Didn't read it properly.

> That means that from the moment you call
>> the static method until the end of the script that object exists.
>> That's probably fine for web-based scripts that don't run for long,
>> but I live in a world where classes often get used in unexpected ways
>> so I tend to write code that's efficient without relying on the
>> environment it's running in to clean it up.

>
> are you saying that the OPs getInstance() method causes each new instance
> to hang around inside memory because php doesn't know that it's no
> longer referenced,
> even when it's used like so:
>
> Test::getInstance()->doSomething();
>
> and that your alternative does allow php to clean up the memory?


I could be wrong, I don't know the internals of PHP well enough to be
definitive, but I'd rather err on the side of caution than write leaky code.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 30/01/2008, 11h45   #19
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Stut schreef:
> Jochem Maas wrote:
>> Stut schreef:
>>> Nathan Nobbe wrote:
>>>> On Jan 29, 2008 7:27 PM, Stut <stuttle@gmail.com> wrote:
>>>>> Personally I'd use a static method in this instance.
>>>>
>>>> thats what i recommended.
>>>>
>>>>> If you need to create
>>>>> an instance of the class you can do so in the static method and
>>>>> that way it
>>>>> will get destroyed when the function is done. Otherwise the object
>>>>> scope is
>>>>> far larger than it needs to be, which IMHO is an unnecessary waste of
>>>>> resources and certainly less aesthetic.
>>>>
>>>> lost you on this part ..
>>>> whether you create an instance in client code by calling new or
>>>> encapsulate the call
>>>> to new in a simple factory method there will still be only one
>>>> instance of the class,
>>>> and it will still be in scope once the method is finished executing,
>>>> because all it does
>>>> is return an instance of the class its a member of.
>>>> maybe you mean something other than what i posted earlier when you say
>>>> static method?
>>>
>>> You posted a singleton pattern.

>>
>> huh? the OPs getInstance() method returns a new object on each call,
>> hardly
>> a singleton is it?

>
> Quite right too. Didn't read it properly.
>
>> That means that from the moment you call
>>> the static method until the end of the script that object exists.
>>> That's probably fine for web-based scripts that don't run for long,
>>> but I live in a world where classes often get used in unexpected ways
>>> so I tend to write code that's efficient without relying on the
>>> environment it's running in to clean it up.

>>
>> are you saying that the OPs getInstance() method causes each new instance
>> to hang around inside memory because php doesn't know that it's no
>> longer referenced,
>> even when it's used like so:
>>
>> Test::getInstance()->doSomething();
>>
>> and that your alternative does allow php to clean up the memory?

>
> I could be wrong, I don't know the internals of PHP well enough to be
> definitive, but I'd rather err on the side of caution than write leaky
> code.


the way I understand garbage collection as it is right now is that pretty much
nothing is cleaned up until the end of the request but that php should be able
to see that the ref count is zero in both cases either way.

IIUC the yet to be released garbage collection improvements will potentially find/destroy
unused zvals sooner (as well as being better in sorting out defunct circular references etc)
but that the garbage collection itself uses a certain ammount of cpu cycles and in short
running scripts (e.g. most of what we write for the web) it's likely to be better to
let php just destroy memory at the end of the request.

that said your more cautious approach cannot hurt :-)

PS - my apologies if the memory related terminology I've used is somewhat bogus - please
put it down to my lack of proper understanding :-/

>
> -Stut
>

  Réponse avec citation
Vieux 30/01/2008, 12h55   #20
Anup Shukla
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Nathan Nobbe wrote:
>> Actually, I don't think so. I believe constructors return void, while
>> the 'new' keyword returns a copy of the object.

>
>
> im pretty sure constructors return an object instance:
> php > class Test { function __construct() {} }
> php > var_dump(new Test());
> object(Test)#1 (0) {
> }
>


AFAIK, constructor simply constructs the object,
and *new* is the one that binds the reference to the variable
on the lhs.

So, constructors return nothing.

> but anyway, how could you even test that __construct() returned void
> and the new keyword returned a copy of the object? new essentially
> invokes __construct() and passes along its return value, near as i can tell.
>
> Christoph,
> if you dont want to write a function in the global namespace, as suggested
> in the article, Eric posted, just add a simple factory method in your class,
> eg.
> <?php
> class Test {
> public static function getInstance() {
> return new Test();
> }
>
> public function doSomething() {
> echo __METHOD__ . PHP_EOL;
> }
> }
> Test::getInstance()->doSomething();
> ?>
>
> -nathan
>



--
Regards,
Anup Shukla
  Réponse avec citation
Vieux 30/01/2008, 13h12   #21
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Anup Shukla schreef:
> Nathan Nobbe wrote:
>>> Actually, I don't think so. I believe constructors return void, while
>>> the 'new' keyword returns a copy of the object.

>>
>>
>> im pretty sure constructors return an object instance:
>> php > class Test { function __construct() {} }
>> php > var_dump(new Test());
>> object(Test)#1 (0) {
>> }
>>

>
> AFAIK, constructor simply constructs the object,
> and *new* is the one that binds the reference to the variable
> on the lhs.


not exactly - 'new' asks php to initialize an object of the given class,
the 'binding' to a variable occurs because of the assignment operator. the __construct()
method is called automatically by php after the object structure has been initialized, so primarily
nothing is returned because the call to __construct() doesn't happen directly in userland code.

at least that's how I understand it.

>
> So, constructors return nothing.
>
>> but anyway, how could you even test that __construct() returned void
>> and the new keyword returned a copy of the object? new essentially
>> invokes __construct() and passes along its return value, near as i can
>> tell.
>>
>> Christoph,
>> if you dont want to write a function in the global namespace, as
>> suggested
>> in the article, Eric posted, just add a simple factory method in your
>> class,
>> eg.
>> <?php
>> class Test {
>> public static function getInstance() {
>> return new Test();
>> }
>>
>> public function doSomething() {
>> echo __METHOD__ . PHP_EOL;
>> }
>> }
>> Test::getInstance()->doSomething();
>> ?>
>>
>> -nathan
>>

>
>

  Réponse avec citation
Vieux 30/01/2008, 14h47   #22
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 30, 2008 5:56 AM, Stut <stuttle@gmail.com> wrote:

> Nathan Nobbe wrote:
> You posted a singleton pattern.



no, what i posted was a simple factory pattern.
if you invoke it twice there will be 2 instances of Test in memory,
eg. not singleton.
$a = Test::getInstance();
$b = Test::getInstance();


> That means that from the moment you call
> the static method until the end of the script that object exists. That's
> probably fine for web-based scripts that don't run for long, but I live
> in a world where classes often get used in unexpected ways so I tend to
> write code that's efficient without relying on the environment it's
> running in to clean it up.



i usually only need to do cleanup in cli scripts that batch large amounts of
data. this is my practical experience anyway.


> This would be my implementation...
>
> <?php
> class Test {
> public static function doSomething() {
> $o = new Test();
> $o->_doSomething();
> }
>
> protected function _doSomething() {
> // I'm assuming this method is fairly complex, and involves
> // more than just this method, otherwise there is no point
> // in creating an instance of the class, just use a static
> // method.
> }
> }
> Test::doSomething();
> ?>
>
> Of course this is just based on what the OP said they wanted to do. If
> there is no reason to create an instance of the object then don't do it.



well you are still creating an instance of the object. the only difference
is
you are forcing it the ref count to 0 by assigning the instance to a local
local variable.

It's fairly likely that I'd actually just use a static method here,


both your and my code use static methods. it sounds to me like you are
using the term 'static method' to mean a static method that has a variable
with a reference to an instance of the class that it is a member of. which
is obviously a particular use of a static method, and therefore a bad
practice
imho. not the technique, mind you, the label of 'static method' for the
technique.


-nathan

  Réponse avec citation
Vieux 30/01/2008, 15h46   #23
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Nathan Nobbe wrote:
>> It's fairly likely that I'd actually just use a static method here,

>
> both your and my code use static methods. it sounds to me like you are
> using the term 'static method' to mean a static method that has a variable
> with a reference to an instance of the class that it is a member of. which
> is obviously a particular use of a static method, and therefore a bad
> practice
> imho. not the technique, mind you, the label of 'static method' for the
> technique.



Actually no, I mean I would *just* use a static method. If there is no
reason to instantiate an object, why would you?

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 30/01/2008, 15h50   #24
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

On Jan 30, 2008 10:46 AM, Stut <stuttle@gmail.com> wrote:

> Nathan Nobbe wrote:
>
> Actually no, I mean I would *just* use a static method. If there is no
> reason to instantiate an object, why would you? <http://stut.net/>



you realize you are instantiating an class in the code you posted, right?
from you post:
$o = new Test();
if i didnt know any better, id call that an instantiation of the Test class

the only thing is you are forcing it out of scope by using a local variable
to store the reference to the object.

-nathan

  Réponse avec citation
Vieux 30/01/2008, 15h53   #25
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How can I do this -- method chaining

Nathan Nobbe wrote:
> On Jan 30, 2008 10:46 AM, Stut <stuttle@gmail.com
> <mailto:stuttle@gmail.com>> wrote:
>
> Nathan Nobbe wrote:
>
> Actually no, I mean I would *just* use a static method. If there is no
> reason to instantiate an object, why would you? <http://stut.net/>
>
>
> you realize you are instantiating an class in t