Results 1 to 4 of 4

Thread: echo nothing?

  1. #1
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default echo nothing?

    Hi,
    I have created a class "friendClass" and tried to echo it. It prints nothing without any errors, not sure what was it going on? Any helps would be appreciated!
    PHP Code:
    class friendClass{
        
            
    //attributes - variable
            
    public $name;
            public 
    $sex;
            public 
    $title;
            public 
    $country;
            
            
    //methods - function
            
    public function eat(){}
            public function 
    watch(){}
            public function 
    drive(){}
            public function 
    speak(){}    
            
        }
        
        
    $friend1 = new friendClass;
        
    $friend2 = new friendClass;
        
    $friend3 = new friendClass;
        
    $friend4 = new friendClass;
        
        
    $friend1 -> name "Bach";
        
    $friend1 -> title "Muscian";
        
    $friend1 -> sex "Male";
        
    $friend1 -> country "Germany";
        
        
    $friend1 -> eat("Bread");
        
    $friend1 -> speak("German");
        
    $friend1 -> drive("Horse");
        
    $friend1 -> watch("Beethoven");
        
        echo 
    "Name is: ".$friend1->name

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    See traq's post below. I misread the code.
    Two things:
    1. Your class is not closed at the end. Add a } if you don't have one. If not, post the whole code so we can help. If you don't have a final bracket, this probably is why nothing is happening: you have a parse error, and your error reporting (at the system level) is not on so that you just see a blank page.
    2. That won't echo anything (or do anything at all) until you create an instance of the class. For example: $myinstance = new friendClass();.

    Also, it's somewhat odd to use an echo statement like that in the definition of a class. It's probably better to put that in the __create() function, which is run by default when a class is created.
    Last edited by djr33; 11-22-2011 at 08:03 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    won't echo anything (or do anything at all) until you create an instance of the class.
    actually, that's what it is: you aren't instantiating the class.
    PHP Code:
    // this
     
    $friend1 = new friendClass

    // should be
     
    $friend1 = new friendClass(); 
    however, Daniel is right that you should be getting some errors from that code. you should check your error reporting settings.

    your methods are empty, so they won't do anything at all. But when you try to pass an argument to them when they don't expect any, you should be getting an error.

  4. The Following User Says Thank You to traq For This Useful Post:

    djr33 (11-22-2011)

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, I completely missed that. I must have been too tired when I read that. Thanks for pointing it out.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •