SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $this as function argument

    Hello,

    Is it ok to pass $this as a argument to a function

    (bad) example:

    PHP Code:
    class Order{

     public function 
    __construct($orderid$user){
      
    $this->OrderId $orderid;
      
    $this->User $user;
     }
      public 
    OrderId;
      public 
    Product;
      public 
    User;
    }

    class 
    User{
     public 
    ID;

     public function 
    GetOrders(){
      
    /*Some funky code to get Orders from database by userID.*/
     
    $orders[] = new Order($row['orderId'], $this);

    }


    I know this works, im just making sure i am doing it correct

    ^^

    Thanks
    Follow the dream, don't chase the competition.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    664
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Passing $this as an argument is fine.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Passing $this is absolutely fine.

    What I don't like is this:
    PHP Code:
    class Order{
     public function 
    __construct($orderid$user){ 
    That will complicate things if, for example, you want to grab an order by an ID regardless of user.


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
  •