user2076753
user2076753

Reputation: 11

Simple PHP OOP syntax calling variables from a class method

I'm mostly a designer and don't have a lot of experience with OOP in PHP, so please be kind as my terminology may not be exactly precise. It's the PHP/OOP syntax that I need the most help with, and I've searched for a solution several times here and on Google as I imagined this would be a pretty straight forward question but haven't found anything to help.

I'm trying to create a class method that pulls client account data from a database using the client's account ID. There are about 20 variables I want to pull and have access to on various pages on my site.

Here's my class file (classfile.php):

class Accounts{

    // Function to get client data from database.
    public function getAccount($acctId){

        require("/var/www/vhosts/aqios.com/httpdocs/00omvat/dbinfo.php");
        mysql_connect("localhost","user","password") or die();
        mysql_select_db("database") or die();

        $query = "SELECT * FROM clients WHERE id='$acctId' LIMIT 1";
        $result = mysql_query($query) or die();
        while($row = mysql_fetch_array($result)){
            $this->$firstName = $row["firstName"];
            $this->$lastName = $row["lastName"];
            $this->$address1 = $row["address1"];
            $this->$address2 = $row["address2"];
            $this->$city = $row["city"];
            $this->$state = $row["state"];
            //etc., etc.
        }

        mysql_close();
    }

}

Here's one of my pages (index.php):

include_once('classfile.php');

$acctId = 111111;
$object = new Accounts();
$object->getAccount($acctId); //Script dies unless I comment this line out.

First of all, the script dies unless I comment out that last line, so something must be wrong with my syntax there. But what I really need to know is how to call and place the city or any other of these variables into my page? I don't know the proper syntax. I can't imagine it would be $object->getAccount($acctId)->$city. I know that's wrong. How would I call the city or any other variable from this object?

Also, I do know that I should be using another method to connect to my database, and I will do that once I get this figured out first.

Thank you in advance for any help you can offer!

Jason

Upvotes: 0

Views: 377

Answers (2)

sebapalus
sebapalus

Reputation: 556

  1. As mentioned by others, you should use $this->firstName instead of $this->$firstName
  2. $this refers to the object, not to the method
  3. It will work without explicit initialization, but you should define for better reading all your object fields as public (or private/protected and write getter methods):

    class Accounts {

    public $firstName;
    public $lastName
    ...
    

    }

  4. With variables defined as in 3, you should be able to refer to their values with $object->firstName

  5. You should probably consider splitting classes into two - Accounts (as a DB proxy) and Account (as a single account data) and in Accounts->getAccount you will create Account object and return it - but this is more of a architectual discussion

Upvotes: 0

Marc B
Marc B

Reputation: 360602

This is incorrect

$this->$firstName = $row["firstName"];
       ^---remove the $

It should be

$this->firstName = $row['firstName'];

And the same for the subsequent lines.

Upvotes: 1

Related Questions