SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 60

Thread: Oop's

  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Oop's

    Starting OOP and I followed a tutorial.. how can i get this to print "hello"

    PHP Code:
    <?php

    class words {
        
        function 
    writeText(){
            echo 
    "hello";
        }
        
    }

    $words = new $words;
    $words->writeText;

    ?>

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $words = new $words;

    should be

    $words = new words;

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As BerislavLopac mentioned

    PHP Code:
    $instance = new words
    and remember to use parenthesis on the method call:

    PHP Code:
    $instance->writeText(); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ripcurlksm View Post
    Starting OOP and I followed a tutorial.. how can i get this to print "hello"

    PHP Code:
    <?php

    class words {
        
        function 
    writeText(){
            echo 
    "hello";
        }
        
    }

    $words = new $words;
    $words->writeText;

    ?>
    You gotta use parenthesis when you call the method; just like a function call.

    $words = new words;
    $words->writeText();

    Also, class names are usually capitalized + camel case. But this is just a matter of common style.

    class Words {

    }

  5. #5
    SitePoint Member
    Join Date
    May 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $words->writeText();

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mwmitchell View Post
    You gotta use parenthesis when you call the method; just like a function call.

    $words = new words;
    $words->writeText();

    Also, class names are usually capitalized + camel case. But this is just a matter of common style.

    class Words {

    }
    That about sums it up, yep.
    TheDrunkenEpic - my ramblings

  7. #7
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Big thanks guys!

    What do you use to write your code? Dreamweaver? Notepad ++? PHP designer?? Eclipse?

  8. #8
    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)
    Notepad++ for me.

    Small hint though. Unless the whole point of the object is to output text to the browser, don't use echo within a class.

    A prefered method would be:
    PHP Code:
    <?php 
    class Words
        function 
    getText(){ 
            return 
    'hello';
        }  
    }
    $words = new Words;
    $words->getText();
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks ark, input appreciated. Just trying to write my first class and was testing. Thx

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EditPlus3
    TheDrunkenEpic - my ramblings

  11. #11
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I use Intype for small scripts (excellent text editor) and an IDE for building really complex applications (such as frameworks or CMS). The best IDE for PHP is probably Zend Studio. Pretty cheap too. NuSphere PhpEd has some nice integration with markup languages though.

    PHPDesigner is weak. Dreamweaver's PHP editor is a joke (well maybe not a joke, but can't be taken seriously in comparison to the others) and PHPEclipse just simply annoys me.

    NuSphere PhpEd has been loyal to me although if I had to pick one from the ones available now for the end of my life I'd pick Zend Studio. They've done it all right there, editor wise. It's just... I dunno. Not very inviting, I guess. But it's up to you.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by CoderMaya View Post
    PHPDesigner is weak.
    Explain?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For one, it barely supports variables/function documention.

    For example in a class I'd do

    /**
    * @var myObject
    */
    public $myObject;

    to let most good IDEs realize that that's the type of this variable, meaning... code insight.

    Not to mention it's extremely annoying. It doesn't wait a second since you type the class keyword and the final '}' before it starts spitting out red lines and supposed errors. You think it'd give you at least 3 seconds before shouting at you with these red flashy and distracting colors. But I guess it has other plans. I always feel like it's mocking me.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  14. #14
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by CoderMaya View Post
    For one, it barely supports variables/function documention.

    For example in a class I'd do

    /**
    * @var myObject
    */
    public $myObject;


    to let most good IDEs realize that that's the type of this variable, meaning... code insight.
    The pro version has a good documenter built in, not sure about the free version.


    Quote Originally Posted by CoderMaya View Post
    Not to mention it's extremely annoying. It doesn't wait a second since you type the class keyword and the final '}' before it starts spitting out red lines and supposed errors. You think it'd give you at least 3 seconds before shouting at you with these red flashy and distracting colors. But I guess it has other plans. I always feel like it's mocking me.
    Now this I CANNOT disagree with!!!!!!!!!!!!!
    Sooo damn annoying
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  15. #15
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha. I've only used the (paid) version of 2007 though. Not sure about 2008's documentation support.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  16. #16
    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)
    PHPEd for me, I think. I used to use Zend, but PHPEd has everything Zend does & more, and Zend ran rather slowly on my pc...

  17. #17
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eclipse PDT for me. I love its debugger. Helps me out a lot when I make the common typo.

  18. #18
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Lets not let this thread get into another "I use this - You use that" IDE thread! We have plenty of them already!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  19. #19
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The seed has been planted.

    Anyway, back on topic. When using OOP remember to actually consider your class as an object with actual properties. Try to avoid creating classes that really do nothing and are simply wrappers for nothing special. Then you're creating classes just for the sake of it and it makes your code a lot less logical. A class needs to be an object, like in the real world, the methods manipulate its properties, and the object contains useful info that can be passed around your app. Just some Lami odd theory for you there.

  20. #20
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have to disagree. I think using classes as static function libraries can be very very helpful and convenient at times. It doesn't really use up more resources, too, nor makes the file size bigger by more than a few bytes.

    global functions FTL, is what I'm trying to say.
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  21. #21
    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 doesnt make a difference - have an object in the global space, or have functions in the global space - same thing really.

    Roll on namespaces though.

  22. #22
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, no. Not objects, classes.

    As in to have all the string functions under one roof is very useful, since there's no use in creating a string object.
    PHP Code:
    Class String {
    public static function 
    upper($str) {
    return 
    strtoupper($str);
    }

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

  23. #23
    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... that's exactly the same as using functions then, only you call them in a different way?

    Seems a bit pointless to me :P

  24. #24
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP is known to suck at function names. It's all cluttered up.
    I find this way to be very helpful for keeping my sanity at a decent level.

    And if that seems a bit pointless to you then well.. Hehe. I don't know what to say. What's more important for modern coding than organization and readability?
    Learn about the new Retro Framework
    Code PHP the way it was meant to be coded!

  25. #25
    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)
    I think people make too much of a fuss about the function names. Yes, they are a bit of a mess, but I've never ever found it to be a problem... you just learn what they are, same as any language. Autocomplete in your IDE is also very useful

    I was referring more to use a static class instead of just using functions though. What's wrong with defining all the functions in another file, functions.string.php or whatever? Using a class for it seems very overkill.


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
  •