SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question regarding OOP

    I was attempting to do a few things in PHP5 using OO code.. its just not working out, I could of swore I did this correctly.. Check the code below, and let me know whats going wrong please!

    PHP Code:
    class Player {
      private 
    $user;
      private 
    $player;
      public function 
    __construct($user) {
        
    $query mysql_query("SELECT * FROM players WHERE name='$user' LIMIT 1");
        
    $this->player mysql_fetch_object($query);
        
    $this->setName($this->player->name);
        echo 
    $this->player->name;
      }
      public function 
    setName($name) {
        
    $this->player->name $name;
      }
      public function 
    getName() {
        return 
    $this->player->name;
      }
    }
    dbconnect();
    $player = new Player("Jonathan");
    $player->setName("Bob");
    echo 
    $player->getName
    i have dbconnect() function defined, and thats not the issue, but the code above is.. apparently the getters and setters are not working.

  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)
    mysql_fetch_object doesn't work native as the type of the object fetched isn't the right one, but with a small typecast it works flawlessly.

    PHP Code:
    <?php

    function ClassTypeCast ($obj$class) {
        
        if (
    class_exists($class)) {

            
    $ser '/^O:[0-9]+:\\"[^"]+\\":/i';
            
    $rep 'O:' strlen($class) . ':"' $class '\\":';
            
            
    $obj serialize($obj);
            
    $obj preg_replace($ser$rep$obj);
            
    $obj unserialize($obj);

            return 
    $obj;

        }
    }

    class 
    Player {
        
        private 
    $user;
        private 
    $player;
        
        public function 
    __construct ($user) {

            
    $query mysql_query("SELECT * FROM players WHERE name='$user' LIMIT 1");
            
    $this->player ClassTypeCast(mysql_fetch_object($query), __CLASS__);

            
    #$this->setName($this->player->name); # This makes no sense. Why assign something already assigned?
            #echo $this->player->name;
        
        
    }
        
        public function 
    setName ($name) {
            
    $this->player->name $name;
        }
        
        public function 
    getName () {
            return 
    $this->player->name;
        }


    }

    dbconnect();

    $player = new Player('Jonathan');
    $player->setName('Bob');
    echo 
    $player->getName(); # You need ()
    Last edited by logic_earth; Aug 15, 2007 at 02:05.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome! it works!

    Thanks alot!

    The only thing I caught that you did wrong was down here: $this->player->name = $name;

    since you changed it to fetch_assoc (btw can I use fetch_object instead?).

    Interesting though, thanks alot for that!

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Oh that was a mistake it should be mysql_fetch_object >.<
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!!! -gives you 10$.. monopoly-

    or something..

    Anyways, thats awesome, still learning OOP with PHP, but I think I am doing alright.

    Thanks again logic_earth !!

  6. #6
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using this all properties will become public.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Surgeon View Post
    apparently the getters and setters are not working.
    They work perfectly, you're just missing () on the last line

    PHP Code:
    echo $player->getName(); 
    However, you code doesn't save any data, so the changes will be only visible during the lifetime of the script.

    Quote Originally Posted by logic_earth View Post
    PHP Code:
            $this->player ClassTypeCast(mysql_fetch_object($query), 
    Are you serious about recommending such a dirty hack to a beginner?

  8. #8
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only a beginner at OOP!!

    hah.. um, what would be a nicer solution?


    also..

    iulian - what do you mean?

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Surgeon View Post
    what would be a nicer solution?
    Your own code is a good working basis. We only need slight cleanup and decomposition:
    Last edited by stereofrog; Aug 15, 2007 at 07:33. Reason: removed the code

  10. #10
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a large book on PHP OOP design? I have "Beginning PHP 5 and MySql" which covers (not in detail) oop..

  11. #11
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Surgeon View Post
    iulian - what do you mean?
    In your class, $player and $user properties were declared as private. After using ClassTypeCast() your properties became public.

    Quote Originally Posted by Surgeon View Post
    Is there a large book on PHP OOP design? I have "Beginning PHP 5 and MySql" which covers (not in detail) oop..
    PHP 5 Objects, Patterns and Practice is a good book.

  12. #12
    SitePoint Zealot TheMonk's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Surgeon View Post
    Is there a large book on PHP OOP design? I have "Beginning PHP 5 and MySql" which covers (not in detail) oop..

    Try "Object Oriented PHP" by Peter Lavin. I'm currently working through it and really enjoy the book. It teaches the fundamentals of OOP in php all the way to advanced thecniques and it's focused only on OOP.
    http://www.amazon.com/Object-Oriente...7186396&sr=1-1


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
  •