SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class/object property without declaration...

    In the official php doc i can't find this issue. Anyway i would like to know if this is valid in php OO code:

    Code:
    class Prova {
    
       public function Prova1() {
           $this->property1 = 'ciao';
        }
    
    }
    ...'cause i find this style of code in many places...most in frameworks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Completely valid(creates a public property), but many would call it bad style.

    There's a certain appeal to being able to glance at a class definition and see all the member variables declared outside of the methods, usually all of them in the same spot. Having them all in the same area imo helps the reader to visualize and understand the implementation with less effort, and gives a great place to add any comments that may be warranted for certain variables.

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Completely valid(creates a public property), but many would call it bad style.

    There's a certain appeal to being able to glance at a class definition and see all the member variables declared outside of the methods, usually all of them in the same spot. Having them all in the same area imo helps the reader to visualize and understand the implementation with less effort, and gives a great place to add any comments that may be warranted for certain variables.
    Ok but if you read all the official php doc you won't find any reference to this style right?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't find anything specific, but I did see example code using the feature in the old php4 object docs
    http://www.php.net/manual/en/oop4.newref.php

    And on the subject of uninitialized variables, an example showing them creating a public property on an object(from outside the class though)
    http://www.php.net/manual/en/languag...les.basics.php

  5. #5
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    There isn't a problem per se, but it does cause general issues.


    PHP Code:
    class Foo {
        public function 
    one() {
            
    $this->bar 'test';    
        }


        public function 
    two() {
            return 
    $this->bar;
        }
    }

    $foo = new Foo;
    $foo->one();
    echo 
    $foo->two(); 

    This works but remove the call to one():

    PHP Code:
    $foo = new Foo;
    echo 
    $foo->two(); 
    And you'll get "Notice: Undefined property: Foo::$bar". Do you really want your methods to have to be called in a specific order in most cases?


    Not really an issue on its own but it's not desirable and causes maintainability issues.


    It also gives your API less clarity.

    If you have a large class, you may run into name conflicts without realising it (two functions may use $this->foo for different purposes). If the variables are declared, you can quickly and easily see what names are in use.


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
  •