SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Bad practice?

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bad practice?

    Hi, someone told me it is bad practice to use a member variable for an object handle, can you confirm if so, can you please explain why? Thanks. Here is an example:

    PHP Code:
    class 
        public function 
    hello() {
            echo 
    'hello';
        }
    }


    class 
    {
        
    // wrong to use a member var for handle
        
    private $a;

        public function 
    __construct() {
          
    $this->= new A();
          
    $this->a->hello();
        }




  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Erm...I think you were told wrong...Using an object property to hold another object is perfectly valid. However, using echo (or any output) in an object is bad practice in my book, unless that is the purpose of the object.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Erm...I think you were told wrong...Using an object property to hold another object is perfectly valid. However, using echo (or any output) in an object is bad practice in my book, unless that is the purpose of the object.
    I agree with the echo, I did that strictly as an example. I was told using an object property to hold an object was not "thread safe" and that it would be better to create a method to instead return the instance or create a new instance of the object.. Thanks.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bump, anyone.

  5. #5
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP doesn't do threading...your first example is fine. That's how you hold an object inside another object. Hell a string is technically an object. You assign it to a class variable to keep track of it. As long as it's private. If you want the outside world to access the object you should create an accessor/mutator for it.

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've understood it to be common practice and never heard of it being problematic.
    Seems fine to me.

  7. #7
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't intend to use the instantiated object through out the class then there is no need to have accessible to all member functions and a local variable will suffice.

    I personally dislike writing $this->myVar 20 too many times and may opt to create a local variable instead. But that is only done for the sake of readability.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!


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
  •