/    Sign up×
Community /Pin to ProfileBookmark

trouble with class $this in static self::doThat()

With static functions, is there any way to test class instantiation and then use the $this keyword? I have a logger function and would like to populate $this->log[] if in the browser and just write to the database if it’s a static call.

I’m unsure the best way to test for a browser in _construct() to handle $_REQUEST parameters. Thanks!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 19.2011 — I don't see (yet) what $_REQUEST parameters -- which are super-global and accessible in any scope -- have to do with static/instantiated classes and $this.

In any case, the short answer is that you cannot use $this within a method that is called statically. If you need to use $this, then you probably do [i]not[/i] want to declare that method as static, and instead always access it via an instantiated object. You could, perhaps, use the Singleton pattern for that class, allowing it to sort of emulate the use of static variables, since all objects of that class within a given script would then be references to the same object (the first one to be instantiated).
×

Success!

Help @ponytrouble spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...