SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable scope in class?

    This is my first attempt ever to build a class in PHP, so please bear with me ..

    I don't get any hard errors, but the variable $title doesn't resolve inside the docStart() method; I simply get an emtpy title tag in my html.

    Any suggestions how I can access that variable inside the method?

    PHP Code:
    class Doc {
       
       public 
    $title="TITLE MISSING";
       
       public function 
    setTitle($t) {
          
    $title=$t;
          return;
       }
       
       public function 
    getTitle() {
          return 
    $title;
       }

       public function 
    docStart() {
          
    $str= <<<MYSTRING
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
       <title>
    $title</title>
    </head>
    <body>
    MYSTRING;
          echo 
    $str;
          return;
       }
       
       public function 
    docEnd() {
          echo 
    "\n\n</body></html>";
          return;
       }
    }

    $myDoc = new Doc;
    $myDoc->setTitle("A world of hello!");
    $myDoc->docStart();
    echo 
    "<h1>HELLO WORLD!</h1>";
    $myDoc->docEnd(); 

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi jonas-e,
    $title is a so called member property of your Doc class, which means you need to access them inside your class via this: $this->title
    In your setter method it should therefore be:
    PHP Code:
    public function setTitle($t) { 
          
    $this->title=$t
          
    // return; no return here, setters should return nothing usually 
       

    Accordingly it should be $this->title in your docstart() method.

    Because you made the title property public, you can access it outside of your class by using your class instance: $myDoc->title, but for best practice member properties should not be public.
    Which means you should set private $title ="..." in your class and access it from outside through a getter method:
    PHP Code:
    public function getTitle(){
    return 
    $this->title;

    --------------------------
    Karsten Rieger
    http://www.tritium-design.de

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Hi jonas-e,

    I'd like to direct you to the portion of the PHP Manual on Classes and Objects (in PHP 5). In there is an extensive overview of everything to do with classes. Of particular note to your problem would be the sections entitled "The Basics" which covers the $this pseudo-variable and "Properties" which covers what properties are all about and how to work with them.
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The other posts should answer your question, but I just wanted to add it's considered better practice to not echo directly from your class methods. Instead they should return the value and the calling code should echo.

    example
    PHP Code:
    class Doc
    {

        
    // snip....
        
        
    public function docStart() {
          
    $str= <<<MYSTRING
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
       <title>
    $this->title</title>
    </head>
    <body>
    MYSTRING;

          return 
    $str;

       }
    }

    $myDoc = new Doc;
    echo 
    $myDoc->docStart(); 

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your thorough help!

    @(fidel karsto): I did intend to make the $title variable private - only tried out the public option to see if it would solve my problem.

    I will try calling "$this->title" inside my string next. But how would PHP know that the "->title" is not just another part of the string ..?

    @Salathe: Thanks, I shall read the manual.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi jonas-e,

    when your are echoing a string in php with double quotes ("..."), the interpreter will parse variables inside it which are recognized by the $ sign.
    In this case the variable "$this" is a self reference to the object instance, which means that "->" will be interpreted as an object accessor to a property or method. If for some reason this should not work out, you should try to concatenate the string like this: "....<title>".$this->title."</title>...."
    --------------------------
    Karsten Rieger
    http://www.tritium-design.de


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
  •