SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Instantiation vs. Static Call

    In the example below, a class was called "statically," i.e., no object was created.

    http://pear.php.net/manual/en/packag...de.example.php

    When is this better than explicitly instantiating a clas?

  2. #2
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For example - take a database class which handles all database interaction.

    Your class might look something like this:

    PHP Code:
    class database {

    var 
    $host="host"
    .......
    ......

    function 
    connect() {
    //connect to db
    }

    function 
    updatedb() {
    // update db
    }

    function 
    getSQLDate() {
    // Returns a data in SQL format
    }


    Now - if you want to use updatedb, you would have to initialize the class, because updatedb would have to itself call connect(), and might need to have access to the variables var $host... - it also might want to store information about the queries - you could have a logError() function in case there are any errors, which stores any errors in a string and logs them.

    However, if in some part of your script, you needed and SQL data, you could use database::getSQLDate(); - here there would be no point creating an instance of the class.

  3. #3
    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 jackli
    When is this better than explicitly instantiating a clas?
    It can be easier to use static calls, because you don't have to worry about how you get the object instance. As a rule of thumb though, it's bad practice to use static calls. Unless you can't see any other way, always use object instances. Definitely avoid static properties (variables), which are basically a form of global variables.

  4. #4
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    As a rule of thumb though, it's bad practice to use static calls. Unless you can't see any other way, always use object instances. Definitely avoid static properties (variables), which are basically a form of global variables.
    I agree with the static properties bit - its a big no no. But I find static calls to be very useful sometimes. In my above example, if you just want to get the date in SQL format, it makes much more sense to call the static method than to create an object of the class.

    Static methods are to be used rarely, but they can be very useful.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Static (also known as class) methods are applied to the class, not to the specific object (instance) of that class. Since the point of OOP is to manipulate objects, class methods are rarely used, mainly for "utility" classes, factories and metaclass methods.

    As to your example,
    PHP Code:
    # which one is "better"

    $decoder = new Mail_mimeDecode($input);
    $structure $decoder->decode($params);

    # or

    $structure Mail_mimeDecode::decode($params); 
    the answer is "neither" because of poor design. "decoder->decode" is nonsense, it should be "$message->decode()" or "Decoder::execute($message)" with the first one being preferred.

  6. #6
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why is it bad design?

    Quote Originally Posted by stereofrog

    As to your example,
    PHP Code:
    # which one is "better"

    $decoder = new Mail_mimeDecode($input);
    $structure $decoder->decode($params);

    # or

    $structure Mail_mimeDecode::decode($params); 
    the answer is "neither" because of poor design. "decoder->decode" is nonsense, it should be "$message->decode()" or "Decoder::execute($message)" with the first one being preferred.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought I explained that. "Mail_mimeDecode::decode" - speak it aloud. Did it sound good? Probably no.


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
  •