0

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

1
  • 1
    Do not include in the method, do not initialize the connection there too. Do not use mysql_*. On the other hand, accessing object property $firstName should be done the way: $this->firstName, but first it should be initialized in the class i.e. class Accounts { public $firstName; public $lastName; //... public function getAccount(... Commented Oct 30, 2013 at 14:24

2 Answers 2

1

This is incorrect

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

It should be

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

And the same for the subsequent lines.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestions. It's interesting how no one actually answered my question though. I was asking about the syntax of calling a method variable. Granted, my mysql query wasn't correct, but that wasn't my question. Unfortunately, Stack won't allow me to post the entire working copy because I'm new, but I'll try and remember to do it after 8 hours. Regarding calling the method vars, here's what I needed: $client = $object->getAccount($acctId); echo $client['firstName']; This works!
0
  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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.