Php 在OOP中返回查询值
我正在学习OOP。我的问题基于以下代码。每次在index.php中使用Php 在OOP中返回查询值,php,mysql,oop,Php,Mysql,Oop,我正在学习OOP。我的问题基于以下代码。每次在index.php中使用$query\u value->GetName()或$query\u value->GetId()返回值时 它每次运行MySQL查询都是因为$query\u值 在Class.php中 <?php class OOP{ public $name,$id,$password public function query(){ /* Query Performed ro
$query\u value->GetName()
或$query\u value->GetId()
返回值时
它每次运行MySQL查询都是因为$query\u值
在Class.php中
<?php
class OOP{
public $name,$id,$password
public function query(){
/* Query Performed
rowCount() = 1 */
$this->name = returned row value;
$this->id = returned row value;
$this->password= returned row value;
return $this; // [NOTE THIS ($this)]
}
public function GetName(){
return $this->name;
}
public function GetId(){
return $this->id;
}
}
<?php
$test= new OOP();
$query_value = $test->query();
// now returning values
echo $query_value->GetName();
/// Some Html
echo $query_value->GetId();
?>
它是否每次都运行mysql查询?(因为$query\u值)
不,它不是每次都运行查询。使用此语句$query\u value=$test->query()
,您执行了一次查询,并使用$this->name
、$this->id
和$this->password
设置实例属性
在这些语句中$query\u value->GetName()
和$query_value->GetId()
,只访问OOP
类的getter方法来获取名称和id值,就是这样
它是否每次都运行mysql查询?(因为$query\u值)
不,它不是每次都运行查询。使用此语句$query\u value=$test->query()
,您执行了一次查询,并使用$this->name
、$this->id
和$this->password
设置实例属性
在这些语句中
$query\u value->GetName()
和$query_value->GetId()
,您只访问OOP
类的getter方法来获取名称和id值,就是这样。不,它不是每次都查询,因为初始化$query\u value变量时已经调用了initial query()函数。由于您有一个obejct as$test,因此不需要额外的变量来回送该值
$test = new OOP();
$test->query();
// echo out the values
echo $test->GetName();
echo $test->GetId();
然后,您不需要在query()函数中返回$this,因为$test是对象变量,并且已经设置了这些值。不,它不是每次都进行查询,因为在实例化$query\u值变量时已经调用了initial query()函数。由于您有一个obejct as$test,因此不需要额外的变量来回送该值
$test = new OOP();
$test->query();
// echo out the values
echo $test->GetName();
echo $test->GetId();
然后,您不需要在query()函数中返回$this,因为$test是对象变量,并且已经设置了这些值。否,因为您只调用该函数一次。GetName和GetId是该类的独立方法 但我要做的是像这样调用getter方法:
echo $test->GetName();
echo $test->GetId();
这更容易理解。您的query()方法将这些值归档。您所做的只是将类分配给另一个变量。否,因为您只调用该函数一次。GetName和GetId是该类的独立方法 但我要做的是像这样调用getter方法:
echo $test->GetName();
echo $test->GetId();
这更容易理解。方法的作用是:将这些值归档。您所做的只是将您的类分配给一个不同的变量