SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP return class

    I am calling my class "myclass" and then calling the function "myfirst". At the end of the function I am trying to get the results to be returned to my public variables above. I saw where this was implimented before. I am having trouble figure out why its not working this time around. thanks in advance.

    Code:
    class myclass {
    
    public $w_id;
    public $w_first_name;
    
    
    
    
    
    
    
    function myfirst(){
    
    
    
    $ret = queryf("SELECT * FROM wildcats WHERE w_email_address = %s", '?@hotmail.com');
    
    
    $row = mysql_fetch_assoc($ret);
    
    
    return $row = new myclass($row);
    
    }
    
    
    function s(){
    
    echo $w_id;
    }
    
    
    
    }

  2. #2
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo $this->w_id

  3. #3
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i tried that already, but the public variables are not being assigned any values from the "return $row = new myclass($row);" statement

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A return statement doesn't assign a value to a variable.
    You can assign values to variables by using the assignment operator(=), obviously.

    The point is, you need to actually do it. php can't guess which values you want in which variables.
    PHP Code:
    $this->w_id $row['w_id']; 

  5. #5
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks, i was just hoping there would be a easier way

  6. #6
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bkflash View Post
    ok thanks, i was just hoping there would be a easier way
    How could it be any easier?
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  7. #7
    SitePoint Zealot bkflash's Avatar
    Join Date
    Apr 2006
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was hoping that by returning a new class with a set array inside it, it would take that array of information and assign it to the public variables which would eliminate me having to type out all the variables and assigning the information to them.

  8. #8
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bkflash View Post
    I was hoping that by returning a new class with a set array inside it, it would take that array of information and assign it to the public variables which would eliminate me having to type out all the variables and assigning the information to them.
    You could write a trivial method to do something similar.

    PHP Code:
    class SomeClass
    {   
        public function 
    setVars(array $array)
        {                                      
            foreach (
    $array as $key => $value) {
                
    $this->$key $value;
            }
        }

    PHP Code:
    $data = array('foo' => 'bar''baz' => 'bat');

    $sc = new SomeClass();
    $sc->setVars($data);

    echo 
    "$sc->foo\n"// bar
    echo "$sc->baz\n"// bat 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.


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
  •