SitePoint Sponsor

User Tag List

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 87
  1. #26
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In most cases, I just use if (!$obj->method()) to check for an empty array, 0, null or false. If you know you are expecting other types, then it's obvious to check for them. You should know what types your functions are supposed to return before you call them.

    @arkinstall, if you are expecting possible "issues" which affect the flow of your application, you are probably better to use exceptions rather than multiple return types.

    @OP, I'd say it depends on the complexity of the function. Sometimes really basic functions are better grouped into a small library class and used together. Sometimes they are better by themselves. More complex functions I prefer broken down into simple classes that I can easily re-use or modify. As stated, one of the other benefits are all the class features like autoloading, extending, and also replacement down the road when you want to switch the class with another (dynamically or permanently).

    Use your own judgement, but remember when you create regular functions, you are setting that in stone, and polluting the global namespace more and more. It's best to keep things as isolated as possible.
    Adrian Schneider - Web Developer

  2. #27
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function redirect ($strURL) {
      
    session_write_close();
      
    header('Location: ' trim($strURL));
      exit();
     }
    //function 
    I think I would go with $response->redirect($url);

  3. #28
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    This has very little to do with the original question, but that function is completely pointless. empty is a language construct in php that specifically won't raise an error if a variable isn't set. Putting it inside a function like that, completely counters that feature, since you would then get a warning when calling the wrapping function on a undefined variable. FWIW, I find empty a weird concept in the first place. If you don't know if a variable is set or not, then I think that is a sign of problems with your code, that you should fix instead. As it happens, I'm guessing you feel the same way, because if you had indeed had undefined variables, then you would have realised that your function doesn't work.
    Yeh, I do only ever use it on things that are set to be honest, but I do find myself using empty quite a lot, and having the convenience of being able to use it on a function return is quite useful.

    Back to the original point though, I am generally against using classes as a way to group functions together, when it has no state - ie, the methods are all / should be static - seems really pointless to me.

  4. #29
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, but you can do it in other ways where it does make sense. For example: making it into a class that implements an interface, or to be used with your application in other ways.

    Obviously doing it for the sake of making it a class is pointless, especially with namespace support in the latest version.
    Adrian Schneider - Web Developer

  5. #30
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always write my libraries as classes for the simple reason that it prevents function name clashes
    chrome is a wrapper that combines a browser with spyware

  6. #31
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -T- View Post
    I always write my libraries as classes for the simple reason that it prevents function name clashes
    No it doesn't. Presumably they would be static methods in the class, so they are just as much in the global namespace as a function.

  7. #32
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    No it doesn't. Presumably they would be static methods in the class, so they are just as much in the global namespace as a function.
    PHP Code:
    <?php

    class TestClass1
    {
        
      public static function 
    test()
      {
        echo 
    "TestClass1";
        
      }
    }

    class 
    TestClass2
    {
        public static function 
    test()
        {
            echo 
    "TestClass2";
        }
    }


    //test(); 
    /* uncommenting test() gives a fatal error 
     PHP Fatal error:  Call to undefined function  test() in ...
    */
    TestClass2::test();
    TestClass1::test();
    outputs: TestClass2TestClass1
    chrome is a wrapper that combines a browser with spyware

  8. #33
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What does that prove? TestClass2::test() is just as much in the global namespace as a function would be

  9. #34
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is if you override a function in one library by including another library with the same function names. With static classes it means that you don't have to remember all the function names in all the libraries you have. I guess you can always add a prefix on each function.
    chrome is a wrapper that combines a browser with spyware

  10. #35
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So you are basically abusing classes as a namespace?

  11. #36
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you could say that, but since namespaces is something that just arrived for php, most of my libraries were written before this and does not take advantage of it
    chrome is a wrapper that combines a browser with spyware

  12. #37
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    So you are basically abusing classes as a namespace?
    After checking it up, there is no namespace support in php as we speak. php 5.3 is not released as stable yet
    http://wiki.php.net/todo/php53
    chrome is a wrapper that combines a browser with spyware

  13. #38
    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)
    Using classes to mimick namespaces, is kind of pointless, because they miss on important feature of namesapces; Namely the ability to bind them to a different name in the context, that uses them. Without this, static class methods, are just a syntactically different way of making a name more unique. Eg. instead of Foo::test() you could have written foo_test(), and you would have had an equal chance of nameclash. Using a class does perhaps add a little bit of organisational structure to the code, but that's about it. The only real benefit I see, is that they allow for autoloading.

  14. #39
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -T- View Post
    After checking it up, there is no namespace support in php as we speak. php 5.3 is not released as stable yet
    http://wiki.php.net/todo/php53
    No, I know, but that doesn't mean I like using functionality designed for other things in its place!

  15. #40
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Using a class does perhaps add a little bit of organisational structure to the code
    But then even putting groups of related funcitons into different files achieves that

  16. #41
    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 Stormrider View Post
    But then even putting groups of related funcitons into different files achieves that
    Well, you can have protected/private fields for a static class. But that's kind of a weak argument, since your static members really shouldn't have any state. Personally, I prefer using global functions over static methods, if I must have them.

  17. #42
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wysiwyg View Post
    Could someone give an example of such a library function that is so simple it won't benefit from being inside a class?
    I don't personally mind function Libraries, but I do have a static class that groups what MOST of the time are independent functions for generating form elements, that only occasionally share a static member. They are all functions directly related to the task of handling HTML elements like checkboxes and dropdowns that you can't simply autofill by dropping a "value" attribute into.
    Visit my blog
    PHP && Life
    for technology articles and musings.

  18. #43
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Yeh, I do only ever use it on things that are set to be honest, but I do find myself using empty quite a lot, and having the convenience of being able to use it on a function return is quite useful.

    Back to the original point though, I am generally against using classes as a way to group functions together, when it has no state - ie, the methods are all / should be static - seems really pointless to me.
    I use empty for certain GET variables that should never be zero or blank. It helps make certain scripts a little more idiot proof and less hackable in some situations, given that they are an unknown quantity.
    Visit my blog
    PHP && Life
    for technology articles and musings.

  19. #44
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Quote Originally Posted by arkinstall View Post
    Personally I can't stand code that misuses typing. The ! is used to flip a boolean - i.e. true === !false.

    So if(!$var) is like saying "if the opposite of nothing is equal true".

    It may work in practise, but the theory behind it is... well it feels unstable. You'd never see it in stricter languages - and personally I keep my code as strict as possible.
    I disagree. If I only need to check if something is not a specific state, and there is no point in checking if it is in a correct state, then using ! is perfectly acceptable. Many times you may have a function that either returns some sort of information or false on error. If you only need to check for the error condition then you would logically do the following.

    PHP Code:
    if(!doComplicatedStuffThatMightNotWork())
    {
        
    // handle error

    maybe it's lazy, but I don't tend to include an else at the end of that unless it's really necessary.
    Visit my blog
    PHP && Life
    for technology articles and musings.

  20. #45
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hammer65 View Post
    I don't personally mind function Libraries, but I do have a static class that groups what MOST of the time are independent functions for generating form elements, that only occasionally share a static member. They are all functions directly related to the task of handling HTML elements like checkboxes and dropdowns that you can't simply autofill by dropping a "value" attribute into.
    Well, don't you think that there is a point in being able to replace such a class? If you use a static method, your HTML elements will always be the same, but if you use an object instead, you can pass in whatever HTML rendering object you'd like. That way, you can add/change the HTML just by passing in some other object.

    Just an example; say you have some class that renders an HTML paragraph out of a text. Initially, you might think "this will _always_ get rendered as <p>{content}</p>", but then requirements change, and you need to add a link tag to edit the content if the user is an administrator. If you use an object, you would only need pass in some other HTML rendering class that appends the link tag as well. With a static method, you would have to add some conditional inside that method to pull it off. And even worse; your HTML rendering class would need your user object in order to make that conditional possible.

    I guess my point is that; you never know. Some requirement might change, even if someone told you it never would. And given the flexible nature of objects, and the, obviously, static nature of static methods, why risk locking yourself in?

  21. #46
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wysiwyg View Post
    Well, don't you think that there is a point in being able to replace such a class? If you use a static method, your HTML elements will always be the same, but if you use an object instead, you can pass in whatever HTML rendering object you'd like. That way, you can add/change the HTML just by passing in some other object.

    Just an example; say you have some class that renders an HTML paragraph out of a text. Initially, you might think "this will _always_ get rendered as <p>{content}</p>", but then requirements change, and you need to add a link tag to edit the content if the user is an administrator. If you use an object, you would only need pass in some other HTML rendering class that appends the link tag as well. With a static method, you would have to add some conditional inside that method to pull it off. And even worse; your HTML rendering class would need your user object in order to make that conditional possible.

    I guess my point is that; you never know. Some requirement might change, even if someone told you it never would. And given the flexible nature of objects, and the, obviously, static nature of static methods, why risk locking yourself in?
    That is far beyond the intended scope of the class and it's methods. I'm not constructing markup using a DOM parser setup. The concept is to simply allow select-one, combo boxes, checkboxes, radio buttons and groups of radios or checkboxes to be autofilled from values and placed into templates. I've never had a purpose for a system that constructs whole pages of markup, except when working with XML, in which case I use a technique as similar to the standard DOM model as possible.

    If I need to generate a form or table programmatically, then of course I use separate objects, but that isn't the goal in this case.
    Visit my blog
    PHP && Life
    for technology articles and musings.

  22. #47
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    A class is easier to manage when it has less dependencies. By using static methods that directly relate to the class responsibilities you keep all that functionality under one roof making it easier to maintain and integrate into future projects. If your relying on functions outside of the class then you must also bring those functions over to your next project as well. I always try to limit by collaborators whether be functions or objects because the less there are the easier it is to maintain and pull apart individual classes without breaking them. So I think static methods do have their place if they are used within the scope of the class or have functionality/responsibilities that directly relate to the class purpose. I don't think there is anything wrong with placing common functionality in classes as long the classes and methods are grouped in a logical manor. For example, naming a class OneRoof and just placing all your common functionality in that class would be inappropriate and misuse of classes. However, if you were to factor out that same functionality into individual classes and those methods directly related to the class responsibilities I would see nothing wrong with that. Its all about how you approach it.

  23. #48
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Personally I can't stand code that misuses typing. The ! is used to flip a boolean - i.e. true === !false.

    So if(!$var) is like saying "if the opposite of nothing is equal true".

    It may work in practise, but the theory behind it is... well it feels unstable. You'd never see it in stricter languages - and personally I keep my code as strict as possible.
    I use it all the time in C++ and the theory behind it isn't unsound. If you want the formal definition crack open a discrete math book.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  24. #49
    SitePoint Addict SirAdrian's Avatar
    Join Date
    Jul 2005
    Location
    Kelowna, BC
    Posts
    289
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It makes perfect sense to me.

    "If variable does not evaluate to true"

    With arrays, "if array is empty"
    With numbers, "if number is not zero"
    With strings, "if string is not empty" (or "0", but in that case I'd check for 0)
    etc.
    Adrian Schneider - Web Developer

  25. #50
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It works, and there isn't a problem with using it, but I just prefer reading isempty() - when I'm reading the code back, I know I am checking that something is empty, rather than something being false, or 0, or whatever. Makes it much clearer what the code is doing.


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
  •