/    Sign up×
Community /Pin to ProfileBookmark

OOP In PHP: "Static Variables" & "Static Methods"

Hello..

Here’s one of my few questions I will post to get a better understanding of OOP.

When is it necessary to use [I]static variables[/I] and [I]static methods[/I] with OOP in PHP? I’m trying to understand the method behind this. I’m open to all kinds of explanations and code representations as well. Thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 18.2011 — Static variables are used when you want all instances (objects) of that class to be working with the same variable. If one instance changes its value, all other instances will then see it as that value. Additionally, it allows you to access that value without the need of an instantiated object, simply by using the [B]className::$variableName[/B] syntax.

Static methods give you the same access ability: calling the method without an object by using [B]className::objectName()[/B] syntax. This only makes sense (and I think throws a warning or error or exception?) if the static method only references static class variables -- i.e. [b]$this->variableName[/b] would not be legal within that method.

I've not really had much need to use static variables or methods. The few times I have, it's usually been to provide some utility functions wrapped up within a class of related functions: really not OOP so much as organizing some procedural functions in an OOP disguise. ?
Copy linkTweet thisAlerts:
@ChuckBauthorJan 18.2011 — Appreciate that NogDog...

...I don't want to do to much, but I'm about to throw another question out there...please feel free to answer...?
×

Success!

Help @ChuckB 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.13,
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,
)...