SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Enthusiast Jumping Bean's Avatar
    Join Date
    Oct 2007
    Location
    In the air
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using classes without objects?

    Hi,

    I'm trying to get my head around classes and objects in PHP and have a question...

    Am I right in thinking the following is valid:

    PHP Code:
    class SomeClass {
        public static function 
    SomeFunction(){
            do 
    something;
        }
    }

    $something SomeClass::SomeFunction(); 
    So I don't need to instantiate the class with the new operator inorder to use the methods or properties as long as the methods or properties in question are marked as static? Is that right?

    Presumably if that method was not marked as static then the only way I could use it would be:

    PHP Code:
    $something = new SomeClass();
    $something->SomeFunction(); 
    Is that right?

    Thanks.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes and yes.

    You hardly if ever need to use static methods.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Actually, holdI find static classes extremely useful with OOP.

    For example, I have a static class which I call glue. When I instantiate another class, (in the __construct() function) it sends a call to glue::add_object($handle, $this), (handle being its name to access it, $this holding the class itself) which adds the object to an array. Then, other classes all do the same, turning the glue into a server for a network of classes.

    When using a call from any other class, I do it through glue, accessing it through it's $handle. If the class doesn't exist, it sends a call to a listener class called reporter, which I use to write errors to a file. If it does exist, it returns it.

    Very handy for very complex tasks.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ^ Sounds like the Registry would do.

    Personally I tend to be very open-minded to any kind of practice, even if it's not proven to be the very best. Whatever floats your boat as they say. But the only time I used static methods was in multi-threaded programming, which php happens to not support.
    Saul

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Very handy for very complex tasks.
    That's called global scope, essentially no different from global variables.
    It's a very dangerous route to take.

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just want a static function why bother with a class at all. The only use I can think of is the singleton (which is yet another name for a global variable).

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's a very dangerous route to take.
    And how is that?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman View Post
    If you just want a static function why bother with a class at all. The only use I can think of is the singleton (which is yet another name for a global variable).
    AFAIK, a Singleton is by no means a global variable.

    A global variable is a variable whose scope is the whole program, a singleton is an object that can be instatiated only once.

    Basically, a sigleton is achieved by placing a test in the constructor of a class to verify if that class was already instantiated, if it was, returns THAT object, otherwise, it creates it for the first (and last) time and returns it.
    Guilherme Zühlke O'Connor
    www.z-oc.com

  9. #9
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    And how is that?
    Because the scope is too wide, it can lead to name clashes and nightmare-like debugging.

    Besides, if a global variable is changed by some error, you will have to look for that in the whole program what could be quite a lot of code.

    This is particularly problematic when more than one person is developing, but it is always good practice to avoid global variables whenever possible (and by my experience, it always is)
    Guilherme Zühlke O'Connor
    www.z-oc.com

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guioconnor View Post
    A global variable is a variable whose scope is the whole program, a singleton is an object that can be instatiated only once.
    Yes, but the consequence is, that you end up with a global means to access the same instance. Even if the intention was otherwise. That said, I can't think of any class, which you shouldn't be able to instantiate more than once. Except perhaps, if you're hacking around language limitations. But for regular application code, I don't see the need.

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How is it global? All I have is an array holding different arrays, accessble through there handles.

    For example, I can have two objects of the same class, $DB and $DB2, each with connections to different databases. I can then use:
    PHP Code:
    glue::get_object("DB")->query("SELECT [blah]"
    and
    PHP Code:
    glue::get_object("DB2")->query("SELECT [blah]"
    to access completely different databases.

    One handy advantage of this style is when I have an interface, called design. I have two different parent factory classes which implement it, called table_based and div_based. So, when I want to add data from multiple classes, I can just assign a design class to the handle "design", say to the class table_based, and then add rows/columns through the rest of the classes.

    Then, the output will be a table with all of the data I want.

    Say, one day, I decide to upgrade it with a div based layout. all i need to do is change the code:
    PHP Code:
    glue::add_object("design", new table_based); 
    to
    PHP Code:
    glue::add_object("design", new div_based); 
    And it's done.


    The only factor here that makes it global is that once a class is instantiated, it's available to use through other classes. If an object exists, it cant be replaced unless the first one is "killed". It can be changed, but if I come across a problem, I uncomment a line in the static class Reporter, which writes all calls to a file, so you can monitor everything.

    So, without writing more of a descriptive rant, how is that, by any means, dangerous?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    That said, I can't think of any class, which you shouldn't be able to instantiate more than once. Except perhaps, if you're hacking around language limitations. But for regular application code, I don't see the need.
    why would you want more than one instance of a database connection?

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Was that both a retorical and sarcastic question?

    If not, then think about using multiple databases.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Yes, but the consequence is, that you end up with a global means to access the same instance. Even if the intention was otherwise.
    Ok, "by no means" may have been exagerated, but that I don't regard it as the same as a global variable because it will not reside in the global namespace that is one of the great problems of GVs.

    In fact, the singleton class could have a scope narrower than global, I agree singletons can be dangerous, but IMHO they differ in nature.

    Quote Originally Posted by kyberfabrikken View Post
    That said, I can't think of any class, which you shouldn't be able to instantiate more than once. Except perhaps, if you're hacking around language limitations. But for regular application code, I don't see the need.
    You might want to use a resource that has to be unique but can be used by many objects: A connection, a certain GUI, a logfile (see that some examples may be better suited in contexts different than PHP, I am not a PHP expert myself)

    PHP Code:
    $logfile = new LogFile();
    $logfile->write("this happened"); 
    In a chat with several people, you can have many connection objects writing on the same GUI, a singleton would be very handy.

    In an operating system the display can be seen as a Singleton, also a printer, the soundcard...
    Guilherme Zühlke O'Connor
    www.z-oc.com

  15. #15
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Was that both a retorical and sarcastic question?

    If not, then think about using multiple databases.
    I think catweasel meant "Why would you want more than one connection to the same database", that is where a singleton could be very handy.
    Guilherme Zühlke O'Connor
    www.z-oc.com

  16. #16
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    I can't think of any class, which you shouldn't be able to instantiate more than once.
    Session and Response come readily to mind. You'll never have more than one instance of either in a script. I use singletons for classes like those so I don't have to clutter up all my method declarations and calls by passing global objects to every method that uses them.

  17. #17
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    So, without writing more of a descriptive rant, how is that, by any means, dangerous?
    In short, globals introduce action at a distance, strong dependencies, and (worse yet) hidden dependencies. These feats aren't immediately problematic, but tend to make programs more complex and less flexible.

    A fundamental tool to solve problems is, to break it down into smaller, individually independent parts. In programming, this is for example done with functions or objects. For this method to work though, it is imperative that each sub-module is completely isolated from the others. Global scope violates this principle.

  18. #18
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guioconnor View Post
    In an operating system the display can be seen as a Singleton, also a printer, the soundcard...
    And if you want to capture output to a screenshot. Or save the sound to an mp3-file on disk. Or print to a PDF file. How unique are they then?

    Quote Originally Posted by guioconnor View Post
    I think catweasel meant "Why would you want more than one connection to the same database", that is where a singleton could be very handy.
    Let's say that you have a connection open, in which you have begun a transaction. Then you have some data, which should be written outside of transaction, but you can't commit your connection yet. So you need a second connection to the database. It's not a contrived example, since I ended up with a script doing just that, only last week.

    And I haven't even touched on writing unit tests yet.

    Seriously; I can see that it's sometimes easier to use a singleton (because it's a global), but I can't think of one concrete example of the "ensure only one instance" requirement.

  19. #19
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Was that both a retorical and sarcastic question?
    Neither

    If not, then think about using multiple databases.
    guioconnor sums up my post quite neatly.

  20. #20
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Furthermore ...

    Chatting about database *connections* is a little misleading .. I guess what I'm really on about is an app should really only have one instance of a database connection object/layer whatever-you-want-to-call-it.

    You can implement a singleton of your database handling object that allows multiple connections within that object.

  21. #21
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    but I can't think of one concrete example of the "ensure only one instance" requirement.
    Uhm... If you can get away with Request, Session or Response, I can't think of an easy way to implement Registry or Unit of Work. The question might be if you need them.

    There's always a way to avoid it, and perhaps we should, but oh man the sin is so sweet.
    Saul

  22. #22
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Uhm... If you can get away with Request, Session or Response
    Request and Session are perhaps candidates for the "ensure only one instance" part, but you definitely don't want your application to access those through global scope (Eg. be calling a static method). That's something, which should be passed in to the dependants from the outside (In the constructor or as a parameter).

  23. #23
    SitePoint Enthusiast guioconnor's Avatar
    Join Date
    Oct 2007
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    And if you want to capture output to a screenshot. Or save the sound to an mp3-file on disk. Or print to a PDF file. How unique are they then?
    Not unique at all, but then, it won't be the printer nor the soundcard which are.

    The singleton, in this case should be placed for calls to the driver and not as the "file->Print" action on the taskbar.

    Quote Originally Posted by kyberfabrikken View Post
    Seriously; I can see that it's sometimes easier to use a singleton (because it's a global), but I can't think of one concrete example of the "ensure only one instance" requirement.
    I don't think "requirement" is the right word o describe its value. Sometimes easy=good.

    In a more extreme example, we can say we don't need OOP at all, we could do it all procedural, but is easier in OOP and in this case is good, just as it is in the Singleton Pattern, IMHO
    Guilherme Zühlke O'Connor
    www.z-oc.com

  24. #24
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In short, globals introduce action at a distance, strong dependencies, and (worse yet) hidden dependencies. These feats aren't immediately problematic, but tend to make programs more complex and less flexible.
    That's where another static class I call NoClassHandler comes into play. I store the serialized version of each default object in an array, referenced (in the key) by their default handle. Which means if any class wants to access an object which doesn't exist, a fresh replacement is served.

    I think of it as a server in a network. You have a server (the glue class) which client machines (the other classes) try to access each other through. If the client machine which one is trying to access doesn't exist, this server (this is where it differs) has a backup of all it's saved client machine files.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •