Andi Crew
Andi Crew

Reputation: 129

how to get the values of variables php?

So I have:

 $first  = "one";
 $second = "two";

How can I get values of these variables in:

class TD
{
public $first;
public $second;

}

on the contrary, from here:

class TD
{
public $first = "one";
public $second = "two;

}

to here:

    $...   = ... ;
    $... = ... ;

need to then use it in the query(for ex.):

SELECT * FROM table WHERE cell = $first

Upvotes: 0

Views: 32

Answers (2)

user8556290
user8556290

Reputation:

you can also add a constructor and pass an array for multi params

class TD
{
    public $find = [];

    public function __construct($arr){
        foreach($arr as $k=>$v){
            $this->find[$k+1] = $v;
        }
    }
}

$array = ["one", "two", "tree"];
$td = new TD($array);

var_dump($td->find[1]);
var_dump($td->find[2]);
var_dump($td->find[3]);

Upvotes: 1

mega6382
mega6382

Reputation: 9396

Try the following:

$td = new TD;

$first  = $td->first;
$second = $td->second;

The above variable $td is an instance of the class TD and it is accessing the classes properties, which are in this case $first and $second. Which is done like this $td->first. Learn all about OOP in PHP.

Upvotes: 2

Related Questions