SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP - no need to declare member vars?

    reading several OOP tutorials, and i'm told that i must declare the class member variables so that afterwards i can use something like $this->var but i did a test and didn't declare the class member vars and it still worked, here it is the code

    PHP Code:
    <?php

    class MyClass
        
    {
        var 
    $firstvar;
        
        function 
    MyClass()
            {
            
    $this->firstvar "this is the first var";
            
    $this->secondvar "this is the second var";
            }
        function 
    output()
            {
            echo 
    "$this->firstvar";
            echo 
    "$this->secondvar";
            }
        }
        
    $a = new MyClass();
    $a->output();
    ?>
    i only declared the $firstvar, i didn't declare the $secondvar but it still worked, so is it really needed to declare the class member vars?

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    general rule: if it's not supposed to work but it does:
    1) you'll build bad habits
    2) it may be corrected in future versions and break your code
    3) if this happens, the correction probably won't even be heavily documented since it's just a fix and not a version change


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
  •