Jump to content

class methods question


jimfog

Recommended Posts

I wonder, if we want to use the methods/properties of a class. Is it necessary that we must first instantiate this class-meaning making an object out of it?

Link to comment
Share on other sites

no. you can make classes that have static members and methods, so no instantiation is necessary.http://www.php.net/manual/en/language.oop5.static.php

Edited by thescientist
Link to comment
Share on other sites

Though I must wonder what the benefit of creating static members is. Any time I create a property or method of a class, it is intended to be used on objects created from that class. Static methods must be passed all the data they require, so I wonder why not just create a standalone function instead? Or am I missing something?

Link to comment
Share on other sites

And example of a usage for a static property could be to count the number of instances of a class that currently exist. If in each constructor you add 1 to the static property and in each destructor you subtract 1 from the property then it will keep track of the amount of instances that were created.Obviously, there are so many more possible applications for static properties and methods.

Link to comment
Share on other sites

A static method can still access static properties. A class where everything is static (a "static class") is useful for classes that you only need one of, like a database or logging or session or configuration class. If you have a class where you find yourself only ever making and using a single object, then that's a candidate for a static class. One of the benefits of static classes are that they are global, if you're calling static methods or using static properties in any other class or function you don't need to declare it as global. If you're using a single object for things like that then you would need to pass it around or declare it as global or instantiate a new one. Static methods are also used in the factory method of class design.

Link to comment
Share on other sites

Though I must wonder what the benefit of creating static members is. Any time I create a property or method of a class, it is intended to be used on objects created from that class. Static methods must be passed all the data they require, so I wonder why not just create a standalone function instead? Or am I missing something?
If you have static methods accessing (non public!) static properties, it's like you're accessing your own variables that you can be sure no one would touch. This is different from a function that imports globals, since the globals could be modified outside the function.
Link to comment
Share on other sites

As a practical example, consider managing users. You'll have a user class where you can instantiate a new object to represent a single user, but you may also want methods that deal with all users instead of a particular one, like a method to create a new user, look up one or more users and return the objects for each one, etc, which wouldn't necessarily be part of a class that represents a single user. You may have a static class called Users, with methods like createUser or getUser, which would return a User object. e.g.: $user = Users::getUser($uid);echo $user->username;

Link to comment
Share on other sites

Clearly, I have much more to learn about OOP... :P It sounds as if I should have written my current project using a static class, based on some of the replies. Oh well, it works good the way it is and I'm too far along to go back and rewrite it so I'll have to keep this in mind for my next big project. Thanks for the explanations and examples.

Link to comment
Share on other sites

I just made a simple question and it sparked many responses-that is what I call "community".

Link to comment
Share on other sites

In the vein of static classes and objects, there are times when you only want one instance of an object at any one time, yet still want some form of statefulness. For that, there is the Singleton pattern.http://en.wikipedia.org/wiki/Singleton_pattern

Link to comment
Share on other sites

$user = Users::getUser($uid);echo $user->username;
So, to access static methods/properties we use double colons, instead of arrows. Is that correct? Is this the case were we use double colons?
Link to comment
Share on other sites

yes you need to use double colons when you will access static mehod or properties. if you call genearl method or property using :: it will throw strict level error. it is also used when you will use class constant or when you use inheritance in overrde methodshttp://au2.php.net/m...nekudotayim.php

Edited by birbal
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...