SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question How to call a class(echo)

    hi folks i am new at OOP, i created this simple class. its not printing. what am i doing wrong?
    PHP Code:
    class test{
        function 
    world(){
            echo 
    "Hello World";    
        }
    }
    $testing = new test;
    $testing->world() 
    All those who wander aren't lost.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    There's a ; missing at the end of the last line.

    Do you get an error?

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not i don't get any error. i put the ; also
    All those who wander aren't lost.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    add
    error_reporting(E_ALL);
    To the top of the script.
    Then add something like:
    echo "Script is done";
    At the bottom just to make sure the script is actually executing.

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It was a port error. my ports were conflicting. now its working. although i i wanna know what these two steps do in simple english
    PHP Code:
    $testing = new test;
    $testing->world() 
    All those who wander aren't lost.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    line 1 instantiates an object (fires it up)
    line 2 tells the object to now fire up a function, called world

    So you are in fact now delegating some responsibility to that new object you called "testing".

    The blueprint for this object was defined in your class.

    Generally speaking though, in your blueprint you tell your methods (functions) to return stuff rather than echo things.

    PHP Code:
    class test{
        function 
    world(){
            return 
    "Hello World";    
        }

    Because who is to say that in all future cases that when you instantiate the object, and tell it to do something, that you want it to echo the result onto the page in that exact place?

    PHP Code:
    $testing = new test;
    echo 
    $testing->world();

    // OR


    $testing = new test;
    $msg $testing->world();

    // then maybe miles away ...

    echo '<h1>' $msg '<h1>' PHP_EOL
    By having your class echo things it can be thought of as "taking on too much responsibility", so, delegating authority, assigning responsibility these are typical design issues you will need to grapple with in order to gain a better understanding of OOP.

    Sorry for the long post, just wanted to see what this auto-save discussion was about. Seems nice.


Tags for this Thread

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
  •