SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 60

Thread: Oop's

  1. #26
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why? Maybe it's because I'm an OOP-whore. Doing it this way does not require more CPU power or processing time, and whether you like it or not or whether you need it or not, it is simpler and more convenient.

    Sure, I don't need it. But I choose to use it because it makes my life happier, and that's a good enough reason for me.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  2. #27
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh and I think you didn't quite get me. I was not just talking about putting all the string functions in one class (although that could also be useful in an IDE), I was talking also about renaming them.

    For example, explode and implode... A few good years back I was afraid of these functions and had no idea they had anything to do with strings.

    Today, I just rename all inner functions to more appropriate names and store them in a relevant static class.

    string::upper
    string::split
    string::join
    string::sub

    etc etc.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  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)
    Yes, I understood that much :P

    I like OOP as well, but I also advocate using it in the right situations, rather than 'whenever possible'.

  4. #29
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why?
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  5. #30
    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)
    Because it is a tool, and there is no point in using the wrong tool for the job.

  6. #31
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CoderMaya, that's the type of thing I was referring to. Why oh why would you want to create a class to encapsulate existing PHP functioning? It's the same as the days when people created database classes that simply renamed the mysql functions (they still do that actually). Yes, it doesn't do anything to your resources but it is a complete waste of time.

    But let's accept your class for a moment. You've not implemented it right in the first place. It's supposed to be an object, not an encapsulation. So, your string class should actually keep a reference of the string it's holding. So in practice, you should use your string object like this (if it's coded this way, which it actually should be)

    Code PHP:
    <?php
      $myString = new String('Hello World!');
      $myString->replace('Hello', 'Bye');
    ?>

    I'm not sure whether you can overload the string class, or use some fancy coding so that you could simply do $myString = 'Hello'; and still have the object methods.

    You get my point though.

  7. #32
    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 doesn't do anything to your resources but it is a complete waste of time.
    Wrong. Static functions are perfectly fine for renaming badly-named current functions.

    For example, strpos - I hate that (not that function, it's name and many others). String_Position would be much better - abbreviations are just plain annoying.

    Better examples are the very bad array function parameters. Some have haystack first, others have needle first. Personalised functions could fix this in your favour.

    Thirdly, the PHP guys don't keep to a naming standard. HtmlEntities() and, it's partner, Html_Entity_Decode()? ouch.

    However, we're moving away from the topic - the OP is asking about OOP - static classes don't count.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #33
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Wrong. Static functions are perfectly fine for renaming badly-named current functions.
    Not wrong.
    If you can't remember the different function names, or your IDE doesn't have auto completion, or you don't keep a copy of the reference manual open while coding, then I can see why you want to use static functions to reinvent the wheel. Using the argument that the names are imperfect (I agree) and that you will fix it in one fowl swoop with an amazing static class is over the top. Another reason I just accepted the fact that the names are different, and got used to them is because of my involvement in open source projects, and then commercial projects. Those sites don't necessarily use a magic string class, and in all likelihood, if you come in there wanting to change everything, you're off the team.

    Going against the grain and basically rebelling against standard php function names is going to do you more harm than good in my opinion.

    Also, static classes are perfectly on topic, if you read my entire post, you would have noticed that I mentioned a method to actually make a static string class fall into the realm of OOP.

    One final thing. It's really arrogant to come out and say someone's opinion about a coding method is wrong from the outset. Saying someone is wrong, is implying that you're correct, and your method is the only way of achieving something. Over my ten years of coding experience, I have never told someone that what they're doing was wrong, I learnt from what they did and it in turn evolved my own coding style into what it is today. If I had told everyone I taught that everything they did that was against my own coding style was wrong, I would have bred a pack of coders that never thought to challenge the norm, or never think outside of the box. And we don't want robotic coders!

  9. #34
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    so... that's exactly the same as using functions then, only you call them in a different way?

    Seems a bit pointless to me :P
    it would be akin to using a switch over an if/else, when if/else can accomplish the same task.

    I still see it as better organization, but yes they are not really different

  10. #35
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, I completely disagree about you saying OOP is a too. If that's how you look at it, I wouldn't be surprised if you almost never used it for big projects. And by big I mean big.

    OOP is not a tool. It's a modern style.

    Take a look at the (extremely popular) Ruby with Rails. Haha. Let's see you go to a Rails community and tell them to not use OOP all the time because It's just a tool.

    You guys, take your heads out, it's not that we (currently referring to arkinstall and myself) use these static libraries because we're not as good as you and can't remember the function names as it seems like you're trying to imply. It's because we have a more logical choice and, as homo-sapians, we use it.

    It's not that we don't remember the function names. In my case, it's just that I like reading (and writing) my code when the naming convention is repeatitive.

    And arkinstall, what I hate more than the naming convention is that they change parameter placement with each function. Best example: array-value functions. One time the first parameter is array, one time it's the value or key... And that's just irritating.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  11. #36
    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 what happens when you hand your code over to someone else, both of you? They now have 3 sets of string functions to worry about and not know the name of, let alone 1.

    And I am a strong OOP advocate, as I said above. I use it on large and small projects, but I use objects to represent... objects. I think it's a bit of an abuse of the OO principle to have an entirely static class only for holding functions, when you can just have a bunch of functions instead.

    If you want to make things easier for yourself, then fine - do it your way, whatever. But it makes it less maintainable in my opinion, when it really isn't any effort to just learn the php versions of each function. I don't think 'the names annoy me a bit' is a good enough reason for me to rewrite entire categories of function names for a project.

  12. #37
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But here's the thing - unless the coder can't speak a word of english (which probably means he doesn't know much about PHP anyway) then he would understand exactly what the code does.

    strtoupper, for someone who doesn't know PHP, on first sight looks like a random string.

    But string::toUppercase, for example, would always be obvious - to everyone. You want to tell me that a hired PHP programmer wouldn't understand what it means?
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  13. #38
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another excellent use of re-writing these built in functions is because PHP mostly disables support for certain functions between version updates. Re-organizing these functions allow you to fix or if it's needed, re-write your own function so that your scripts will stay functional even if a built in function is deprecated.

    And instead of switching it in who knows how many files, you only need to change it in one place.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  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 CoderMaya View Post
    But here's the thing - unless the coder can't speak a word of english (which probably means he doesn't know much about PHP anyway) then he would understand exactly what the code does.

    strtoupper, for someone who doesn't know PHP, on first sight looks like a random string.

    But string::toUppercase, for example, would always be obvious - to everyone. You want to tell me that a hired PHP programmer wouldn't understand what it means?
    Are you telling me that maintenance of code involves nothing more than reading it?

  15. #40
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I'm telling you that understanding the code involves nothing more than reading it.

    That is, of course, if you write obvious codes and use good naming conventions as I suggest.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  16. #41
    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)
    Or you could use the php conventions, which are worldwide and well known

  17. #42
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Or you could use the php conventions, which are worldwide and well known
    Let me complete your sentence, since I noticed you didn't end it with a period.

    "Or you could use the php conventions, which are worldwide and well known for their flaws and inconsistency."
    And of course I could use them. I don't see your point. Yes, I could. But I choose not to.

    You could jump off a bridge if you want. That doesn't make it the best solution. It's just the fastest one. Just like using the pre-defined function names.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  18. #43
    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)
    Inconsistency, yes. Flaws? I think there are bigger flaws in everyone creating their own system instead, it just makes the problem worse for everyone else who sees someone else's code and has to content with yet another set of names for all the string functions.

    You are completely missing my original point anyway - if you want to create your own string functions or whatever, then go ahead. My point was that using a static class to do it is a bit strange when you could just use standard functions. Seems a bit of an abuse of the OO principle to have an object just for creating functions, it's a bit strange to me.

  19. #44
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Strictly for PHP 5.3 I started to encapsulate PHP's string functions into a String namespace. Now I'm not doing just plan renaming of some of the functions, I'm working on building in UTF-8 support directly into them, since I work using UTF-8.

    While I am at it, it was a good opertunity to reorder some of the argument. I havn't currently finished it.
    PHP Code:
    <?php

    namespace PHP::Common::String;
    const 
    UTF8_AWARE 0x80000000;

    #-------------------------------------------------------------------------------

    const CHANGE_ALL_CASE   1
        
    CHANGE_FIRST_CASE 2
        
    CHANGE_WORDS_CASE 4;

    function 
    toLower $s$f )
    {
        if ( 
    $f === || $f ) { return ::strtolower$s ); }

        if ( 
    $f ) { $s = ::lcfirst$s ); }
        if ( 
    $f ) { $s Arrays::_w$s )->map'::lcfirst' )->join' ' ); }

        return 
    $s;
    }

    function 
    toUpper $s$f )
    {
        if ( 
    $f === || $f ) { return ::strtoupper$s ); }

        if ( 
    $f ) { $s = ::ucfirst$s ); }
        if ( 
    $f ) { $s = ::ucwords$s ); }

        return 
    $s;
    }

    #-------------------------------------------------------------------------------

    const TRIM_BOTH              1
        
    TRIM_LEFT              2
        
    TRIM_RIGHT             4
        
    TRIM_KEEP_DEFAULT_MASK 8;

    function 
    trim $string$mask null$flags )
    {
        if ( !empty( 
    $mask ) ) {
            
    settype$mask'string' );
            if ( 
    $flags ) { $mask .= "\x00..\x20"; }
        } else { 
    $mask "\x00..\x20"; }

        if ( ( 
    $flags & ~) === || $flags ) { return ::trim$string$mask ); }

        if ( 
    $flags ) { $string = ::ltrim$string$mask ); }
        if ( 
    $flags ) { $string = ::rtrim$string$mask ); }

        return 
    $string;
    }

    #-------------------------------------------------------------------------------

    const REPLACE_IGNORE_CASE 1;

    function 
    replace $s$n$r '', &$c null$f )
    {
        if ( 
    $f ) { return ::str_ireplace$n$r$s$c ); }
        return ::
    str_replace$n$r$s$c );
    }
    Reording the arguments gives the advantage of making a simple object like this:
    PHP Code:
    <?php

    namespace PHP::Common::String;
    class 
    Object
    {
        protected 
    $s;

        public function 
    __call $method$args )
        {
            if ( 
    $method === 'toString' ) { return $this->s; }

            
    $func $this->getFunction$method );
            if ( 
    $func !== false ) {

                
    $args array_unshift$args$this->);
                
    $t call_user_func_array$func$args );

                if ( !
    is_string$t ) ) { return $t; }

                
    $this->$t;
                return 
    $this;

            }
        }

        protected function 
    getFunction $method )
        {
            
    $method __NAMESPACE__ '::' $method;
            return ( 
    function_exists$method ) ? $method false );
        }

        
    #---------------------------------------------------------------------------

        
    public function __construct $s ) { $this->= (string)$s; }
        public function 
    __toString  ()     { return $this->s; }

        
    #---------------------------------------------------------------------------

        
    public function repeat () { throw new Exception'Not Implmented.' ); }
    }

    #-------------------------------------------------------------------------------

    function _s $s ) { return new Object$s ); }
    Usuage example:
    PHP Code:
    <?php

    use PHP::Common::String;
    $s String::_s'        too much darn whitespace.        ' )
        ->
    trim()
        ->
    replace'much''little' )
        ->
    replace'darn' )
        ->
    toUpperString::CHANGE_WORDS_CASE );
    print 
    $s# Too Little Whitespace.
    // sp comment bug
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  20. #45
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not talking about an object. That's useless, I agree. I'm talking about a class.

    IE if we have

    PHP Code:
    class string {
        public function 
    toUpper($str) {
            return 
    strtoupper($str);
        }
    }

    $mystring 'something here';

    // We wouldn't do
    $str = new string;
    $mystring $str->toUpper($mystring);

    // We would do
    $mystring string::toUpper($mystring);

    // Then we could include more functions with more obvious names in our class to handle strings. 
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  21. #46
    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)
    object, class, you know what I mean.

    Why not just use a function? Why does it have to be in a class?

  22. #47
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply for organization. To keep it all under one roof.

    I don't like global functions. They remind me of using keywords to accomplish a task. I just don't like it.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  23. #48
    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)
    Surely you can organise by putting them all in the same file?

    Using a static class is no less global than using functions - both are available globally, you just call them in a slightly different way.

  24. #49
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't mean file organization. I mean code organization.
    And yes, you could of course name all function string_function_name and keep them all in the same file.

    I prefer to name them string::function_name and keep them all in the same class. That way, I know I'll never have to pick another name if by accident that function name already exists. Because it's PHP, so who knows. There might be a function called 'asdpoiaus_the_king' for all we know. Well, either that or 'asdpoiaus_theking'. Depends on the PHP developer's mood the time he named it.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  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)
    ...a good argument for using the original funcitons in the first place. I would say you are far more likely to get a clash using a class name of 'String' than anything else...


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
  •