Classes/Databases

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
ChrisCwmbran
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Oct 22, 2019 6:37 am

Mon Nov 11, 2019 9:31 am

Hey all, I have a problem.

Code: Select all

	class MyClass() {
	
	public $var1 = null;
	public $var2 = null;
	public $var3 = null;
	public $var4 = null;
	public $var5 = null;

	function getVars($customer_id) {
	     	 $query = "SELECT *
        		           FROM table
                      		   WHERE id = ?";
                 return $GLOBALS['db_conn']->processSqlRow("MyClass", $query, $customer_id);
	}
Now effectively I'm trying to do:

Code: Select all

	$foo = new MyClass();
	$result = $foo->getVars(1);
but I don't want "$result" to have values $result->var1 to $result->var5.

I want $foo->var1 to $foo->var5 to contain the values returned from the database.

I've tried:

Code: Select all

	$foo = new MyClass();
	$foo = $foo->getVars(1);
which on the face of it works, but I'm told that the second like of this code fragment actually recreates $foo, rather than getting the values into the original instantiated $foo.

Can anyone help please?

Thanks in advance,

Chris.
ChrisCwmbran
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Oct 22, 2019 6:37 am

Mon Nov 11, 2019 9:39 am

I appreciate btw that I could put:

Code: Select all

	
	function getVars($customer_id) {
	     	 $query = "SELECT *
        		           FROM table
                      		   WHERE id = ?";
                 $result = $GLOBALS['db_conn']->processSqlRow("MyClass", $query, $customer_id);
                 $this->var1 = $result-->var1;
                 $this->var2 = $result-->var2;
                 $this->var3 = $result-->var3;
                 $this->var4 = $result-->var4;
                 $this->var5 = $result-->var5;
	}
but to me that seems silly. I almost want to say:

Code: Select all

	function getVars($customer_id) {
	     	 $query = "SELECT *
        		           FROM table
                      		   WHERE id = ?";
                 $result = $GLOBALS['db_conn']->processSqlRow("MyClass", $query, $customer_id);
                 $this = $result;
	}
but it won't let me! And I also find that:

Code: Select all

	function getVars($customer_id) {
	     	 $query = "SELECT *
        		           FROM table
                      		   WHERE id = ?";
                 $this = $GLOBALS['db_conn']->processSqlRow("MyClass", $query, $customer_id);
 	}
doesn't work either!

It's so fustrating!
chorn
php-forum GURU
php-forum GURU
Posts: 614
Joined: Fri Apr 01, 2016 2:18 am

Mon Nov 11, 2019 11:56 pm

Whatever db-conn you are using must be an external lib because that's not standard PHP. So what is processSqlRow actually doing? If it gives back a class of the defined name, what for do you even need to use MyClass in the first place, instead of just querying the database?
ChrisCwmbran
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Oct 22, 2019 6:37 am

Tue Nov 12, 2019 2:08 am

Code: Select all

 
        /**
         * @param string $class Class name
         * @param string $query SQL Query
         * @param mixed $parameter Optional parameters for PDO query
         * @return mixed $result Returns a single object of type $Class
         */
        public function processSqlRow($class, $query, ...$parameter) {
            try {
                $this->_query = $this->_connection->prepare($query);
                $parameterCount = 1;
                if (count($parameter) != 0) {
                    foreach ($parameter as $value) {
                        if ($value === Null) {
                            $this->_query->bindValue($parameterCount, $value, PDO::PARAM_NULL);
                        } elseif (is_int($value) === True) {
                            $this->_query->bindValue($parameterCount, $value, PDO::PARAM_INT);
                        } elseif (is_bool($value) === True) {
                            $this->_query->bindValue($parameterCount, $value, PDO::PARAM_BOOL);
                        } else {
                            $this->_query->bindValue($parameterCount, $value, PDO::PARAM_STR);
                        }
                        $parameterCount++;
                    }
                }
                $this->_query->execute();
                $result = $this->_query->fetchObject($class);
                return $result;
            } catch (Throwable $t) {
                echo "<pre>";
                echo $t;
                echo "</pre>";
            }
        }
I only did it this way because having the data retrieved by the query being returned as a JSON class seemed neat and tidy.
chorn
php-forum GURU
php-forum GURU
Posts: 614
Joined: Fri Apr 01, 2016 2:18 am

Tue Nov 12, 2019 2:35 am

So your db connection already gives you the data encapsuled in the class provided, you could directly use the db connection without wrapping that in your class again.
ChrisCwmbran
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Oct 22, 2019 6:37 am

Tue Nov 12, 2019 3:18 am

I guess - it just doesn't seem to neat as calling a method of a class.

Is there really no way of saying $this = $result effectively?
chorn
php-forum GURU
php-forum GURU
Posts: 614
Joined: Fri Apr 01, 2016 2:18 am

Tue Nov 12, 2019 3:55 am

What for? Looks like you think about a solution that needs a problem.
ChrisCwmbran
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Oct 22, 2019 6:37 am

Thu Nov 14, 2019 5:42 am

Having thought about it I guess you are right! :)

Thank you for your input!

Chris.
Post Reply