# static/object method

- Thread: https://externals.io/message/10739
- Original archive: https://news-web.php.net/php.internals/10739
- Started by Hans Lellelid on 2004-06-24T01:09:23+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10739 — Hans Lellelid — 2004-06-24T01:09:23+00:00

URL: https://externals.io/message/10739#10739

Hi all,

I have a pattern I'm trying to implement in PHP5 using a class that 
could be called statically or non-statically.  Perhaps "pattern" gives 
this too much legitimacy :)  I can't figure out how to do this & wanted 
to ask if a) it's possible at all and b) if there's been any solution 
discussed for such an approach.

Here's what I'm trying to do, but can't figure out a way in PHP5 (or a 
non-hack way in PHP4):

- AuthorFinder is a class that can be used statically for default 
behavior or instantiated if customized behavior is needed.

static, default behavior, probably accounts for 80% of uses:

$c = new Criteria(Author::NAME, "Leo");
$a = AuthorFinder::find($c);

instantiated behavior for performing customizations:

$c = new Criteria(Author::NAME, "Leo");
$af = new AuthorFinder();
$af->setLimit(5);
$a = $af->find($c);

Is there *any* way to accomplish that in PHP5?  I can't reference $this 
-- even to check isset() (which would probably be misleading anyway) 
from inside a method declared as static.  If I do not declare the method 
static then calling it statically triggers `E_STRICT` error which I want 
to avoid since I'm writing PHP5 code.   Perhaps I'm missing an obvious 
solution, in which case I apologize & probably should have asked 
php-general.

It would be *really* cool if PHP could support this type of pattern, as 
there have been a few cases where I've wanted to apply this in PHP5.  
Perhaps this has already been discussed & I just didn't notice.

Thanks,
Hans

---

## #10741 — Andre Cerqueira — 2004-06-24T01:58:35+00:00

URL: https://externals.io/message/10739#10741 · In reply to: #10739

Hi

I think it should be constructor's job to set up an instance with the 
defaults. (no need for static in this case)
Am i missing something?



Hans Lellelid wrote:
> Hi all,
> 
> I have a pattern I'm trying to implement in PHP5 using a class that 
> could be called statically or non-statically.  Perhaps "pattern" gives 
> this too much legitimacy :)  I can't figure out how to do this & wanted 
> to ask if a) it's possible at all and b) if there's been any solution 
> discussed for such an approach.
> 
> Here's what I'm trying to do, but can't figure out a way in PHP5 (or a 
> non-hack way in PHP4):
> 
> - AuthorFinder is a class that can be used statically for default 
> behavior or instantiated if customized behavior is needed.
> 
> static, default behavior, probably accounts for 80% of uses:
> 
> $c = new Criteria(Author::NAME, "Leo");
> $a = AuthorFinder::find($c);
> 
> instantiated behavior for performing customizations:
> 
> $c = new Criteria(Author::NAME, "Leo");
> $af = new AuthorFinder();
> $af->setLimit(5);
> $a = $af->find($c);
> 
> Is there *any* way to accomplish that in PHP5?  I can't reference $this 
> -- even to check isset() (which would probably be misleading anyway) 
> from inside a method declared as static.  If I do not declare the method 
> static then calling it statically triggers `E_STRICT` error which I want 
> to avoid since I'm writing PHP5 code.   Perhaps I'm missing an obvious 
> solution, in which case I apologize & probably should have asked 
> php-general.
> 
> It would be *really* cool if PHP could support this type of pattern, as 
> there have been a few cases where I've wanted to apply this in PHP5.  
> Perhaps this has already been discussed & I just didn't notice.
> 
> Thanks,
> Hans

---

## #10742 — Hans Lellelid — 2004-06-24T02:32:41+00:00

URL: https://externals.io/message/10739#10742 · In reply to: #10741

Andre Cerqueira wrote:
> > […]
> I think it should be constructor's job to set up an instance with the 
> defaults. (no need for static in this case)
> Am i missing something?
> 
> static, default behavior, probably accounts for 80% of uses:


Well, kinda, yeah.  The other part of the equation is that this class is 
generated, which is what allows it to be static.  The default values for 
things are built into the class as static properties or class constants 
or whatever.

The idea is that most of the time this class is really a static class, 
which needs no object instance to perform 'find' functions.

Of course, you're right, using it non-statically only is a solution, but 
it would be far more elegant (IMO) if that weren't necessary :)

Cheers,
Hans

---

## #10743 — Andre Cerqueira — 2004-06-24T03:39:04+00:00

URL: https://externals.io/message/10739#10743 · In reply to: #10742

Sorry, I probably underestimated the question
I just thought it would make things simple
I'm still learning that OO stuff hehe, thought static would set the 
scope to class instead of object for find.
That way you would have to pass the object as a parameter to the static 
find, so that it would get access to object data (since find is from the 
class, not the object).
And that didn't seem like an elegant way eighter hehe

Probably there are other ways...
I should have waited for other replies, shut up and read hehe, but i 
fail that from time to time...


Hans Lellelid wrote:

> Andre Cerqueira wrote:
> > […]
> 
> > […]
> 
> 
> 
> Well, kinda, yeah.  The other part of the equation is that this class is 
> generated, which is what allows it to be static.  The default values for 
> things are built into the class as static properties or class constants 
> or whatever.
> 
> The idea is that most of the time this class is really a static class, 
> which needs no object instance to perform 'find' functions.
> 
> Of course, you're right, using it non-statically only is a solution, but 
> it would be far more elegant (IMO) if that weren't necessary :)
> 
> Cheers,
> Hans

---

## #10744 — Taco van den Broek — 2004-06-24T07:48:17+00:00

URL: https://externals.io/message/10739#10744 · In reply to: #10739

Hans Lellelid wrote:

> Hi all,
> 
> I have a pattern I'm trying to implement in PHP5 using a class that 
> could be called statically or non-statically.  Perhaps "pattern" gives 
> this too much legitimacy :)  I can't figure out how to do this & wanted 
> to ask if a) it's possible at all and b) if there's been any solution 
> discussed for such an approach.
> 
> Here's what I'm trying to do, but can't figure out a way in PHP5 (or a 
> non-hack way in PHP4):
> 
> - AuthorFinder is a class that can be used statically for default 
> behavior or instantiated if customized behavior is needed.
> 
> static, default behavior, probably accounts for 80% of uses:
> 
> $c = new Criteria(Author::NAME, "Leo");
> $a = AuthorFinder::find($c);

You pass your criteria to your search class as instantiated objects, why 
don't you pass your parameters/config flags as another type of object? 
Maybe like this:

$c = new Criteria(Author::NAME, "Leo");
$f = new ConfigFlag(AuthorFinder::LIMIT, '5');
$a = AuthorFinder::find($c, $f);

You could then check the $f object for any customizations, just like you 
check the $c object for where to search.

> 
> instantiated behavior for performing customizations:
> 
> $c = new Criteria(Author::NAME, "Leo");
> $af = new AuthorFinder();
> $af->setLimit(5);
> $a = $af->find($c);
> 
> Is there *any* way to accomplish that in PHP5?  I can't reference $this 
> -- even to check isset() (which would probably be misleading anyway) 
> from inside a method declared as static.  If I do not declare the method 
> static then calling it statically triggers `E_STRICT` error which I want 
> to avoid since I'm writing PHP5 code.   Perhaps I'm missing an obvious 
> solution, in which case I apologize & probably should have asked 
> php-general.
> 
> It would be *really* cool if PHP could support this type of pattern, as 
> there have been a few cases where I've wanted to apply this in PHP5.  
> Perhaps this has already been discussed & I just didn't notice.
> 
> Thanks,
> Hans

---

## #10745 — Alan Knowles — 2004-06-24T08:04:26+00:00

URL: https://externals.io/message/10739#10745 · In reply to: #10744

the `E_STRICT` warnings on static calls to non static methods are probably 
usefull, (as $this could be defined totally randomly), so it does make 
some sense..

btw: does that mean:

class a { function find() { } }
class b extends a { function find() { } }

// call the toplevel parent method.. (skip the imediate parent)
class c extends b { function find() { a::find() } }
$a = new c;
$c->find();

illicits and `E_STRICT` warning???

(sorry I havent got the latest PHP5 compiled ATM..)

Regards
Alan



Taco van den Broek wrote:
> Hans Lellelid wrote:
> 
> > […]
> 
> 
> You pass your criteria to your search class as instantiated objects, why 
> don't you pass your parameters/config flags as another type of object? 
> Maybe like this:
> 
> $c = new Criteria(Author::NAME, "Leo");
> $f = new ConfigFlag(AuthorFinder::LIMIT, '5');
> $a = AuthorFinder::find($c, $f);
> 
> You could then check the $f object for any customizations, just like you 
> check the $c object for where to search.
> 
> > […]
> 
> 


-- 
Can you help out?
Need Consulting Services or Know of a Job?
http://www.akbkhome.com

---

## #10751 — Hans Lellelid — 2004-06-24T11:10:11+00:00

URL: https://externals.io/message/10739#10751 · In reply to: #10745

Alan Knowles wrote:

> the `E_STRICT` warnings on static calls to non static methods are probably 
> usefull, (as $this could be defined totally randomly), so it does make 
> some sense..
> 
> btw: does that mean:
> 
> class a { function find() { } }
> class b extends a { function find() { } }
> 
> // call the toplevel parent method.. (skip the imediate parent)
> class c extends b { function find() { a::find() } }
> $a = new c;
> $c->find();
> 
> illicits and `E_STRICT` warning???

Hmm --yes unless you declare a::find() as a 'static' method.  The good 
news is that static methods can be used on objects w/ no errors (so you 
can definitely accomplish that design, but would just have to declare 
methods as 'static').

So, basically:
   - if you try to call a "normal" method statically you will get 
 `E_STRICT` notice
   - if you make any reference to $this in a 'static' method you will 
get E_FATAL error

(Correct me if I'm missing anything)

Hans

---

## #10752 — Hans Lellelid — 2004-06-24T11:13:28+00:00

URL: https://externals.io/message/10739#10752 · In reply to: #10744

Taco Van Den Broek wrote:

 > Hans Lellelid wrote:
> > […]
 >
 >
 > You pass your criteria to your search class as instantiated objects, why
 > don't you pass your parameters/config flags as another type of object?
 > Maybe like this:
 >
 > $c = new Criteria(Author::NAME, "Leo");
 > $f = new ConfigFlag(AuthorFinder::LIMIT, '5');
 > $a = AuthorFinder::find($c, $f);
 >
 > You could then check the $f object for any customizations, just like you
 > check the $c object for where to search.

That's a good suggestion -- probably the cleanest design possible in 
current PHP object model.

Thanks,
Hans
