Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static variables and functions 1

Status
Not open for further replies.

Geates

Programmer
Aug 25, 2009
1,566
US
I'm hesitant about using static variables and functions in classes. It seems as though they are becoming commonplace for simplicity sake - but there are always two sides to a coin. What is your argument and why?

-Geates
 
It seems as though they are becoming commonplace for simplicity sake

Not particularly for 'simplicity', ... Using static methods and static property values are just commonplace in any Object Oriented Programming methodology or language.

I'm not sure why you think there should be any argument for or against, it's purely a means of controlling inheritance and scope of properties and methods when creating a class or instantiating a child class.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Unless you plan on having classes that will not be instantiated at any time, but you still want to use their methods, there's no real reason to want to have static functions.

If you plan on having an instantiated object of a specific class, then static functions really are quite meaningless.

Static functions themselves are not really any more or less simple to use than normal public or private methods in a class. It's just the context you want to use them in that should dictate whether you use them or not.



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
I am under the impression that defining something as static is to define the var or function in a global scope. One of the first things I learned in programming is not too define things in a global scope (of course, that was back when qbasic was widely used). I was thinking about it last night and, isn't defining a function outside of a class essentially defining it in the global scope? Doesn't a static method allow use without instantiation - hmm...this sounds eerily like a regular function? Personally, I like the idea of instantiating and passing objects(). Speaking of, where do objects get created? Upon executing its declaration or in it constructor?

-Geates

 
where do objects get created?

Where???? Or do you mean when?

But to answer both;
Where; is in application memory space.
When; is as they are called into existence.

Static properties in OOP are more akin to constants in procedural programming rather than being 'global' variables.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
There's a difference between a static variable and a static method.

Though making them static, only has real meaning inside an actual class.

Defining a static variable is not the same as defining it in the global scope because it still only exists inside a specific class.

For example:
Code:
class testingstatic
{
	static $thisvar;
	public $othervar;
	public function __construct()
	{
		
	}
}

$thisvar is static in relation to the other variable in that object, but still needs an object reference to be accessed. $thisvar would not be directly available outside the class its defined in. You would need to actually specify the class this variable belongs to before you can use the variable.

You need to address it as: testingstatic::$thisvar or classname::$varname. Note that you need to include the $ variable identifier since its not part of an existing object or a constant.

defining a static method also requires a class name to access. While they can be accessed anywhere the class is defined, they are not truly global.

Also since there is no object instantiated, static methods cannot make use of the $this variable, nor can they access any of the object properties that are not also defined as static. PHP allows you to access non-static methods statically, but issues a warning stating it should not be done.

Basically defining variables as static should not be confused with global definitions. As static variables and methods still require the existence of a class definition to be meaningful. They do not exist in the global scope, but only within the scope of the class that contains them and its availability.

Defining a variable as static, (or public, private or protected for that matter) outside of a class definition has no meaning.

In structured programming there is no concept of static-ness. Everything is, as it is defined.

A static variable is also not quite a constant, since it can be changed at any time as opposed to a constant which remains the same during execution once it has been defined.




----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Vacunita, that is an awesome answer to a question I could not articulate!

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top