php类里面的变量(php class inside variable within class)

编程入门 行业动态 更新时间:2024-10-17 17:19:47
php类里面的变量(php class inside variable within class)

我不是一个有经验的程序员,我试图理解我与我创建的类有关的问题。

我在A类中有一个私有变量,这个私有变量将查询结果存储到mysql db中,并通过A类中的公共函数访问该变量。

我的目标是通过公共函数回显私有变量的值。 我的问题是这个私有变量在存储查询结果时在类中转换。

这里是私有变量的var_dump

object(stdClass)#4 (1) { ["count(*)"]=> string(1) "8" }

我真的很困惑如何回应数字“8”

我试着这个:

echo $this->private

但正如预期的那样php不会像那样工作并告诉我这个:

"Object of class stdClass could not be converted to string"

谁能指出我正确的方向?

这是代码:

class Quiz { private $db; private $query; private $questions; private $answers; private $total; public function __construct(){ $this->db = new connection(); $this->db = $this->db->dbConnection(); } public function NumRows ($quiz) { $this->query = $this->db->prepare("Select count(*) FROM ".$quiz.""); $this->query->execute(); $this->total = $this->query->fetchObject(); var_dump($this->total); echo $this->total; } } $quiz = new Quiz(); $quiz->NumRows("01_ospf");

im not an experienced programmer and im trying to understand a problem im having with a class i created.

i have a private variable in class A, this private variable stores the result of a query to mysql db, and im accessing this variable through a public function in class A.

My goal is to echo out the value of private variable through the public function. My problem its that this private variable is converted in a class when it stores the query results.

here the var_dump of private variable

object(stdClass)#4 (1) { ["count(*)"]=> string(1) "8" }

im realy confused on how to echo out the number "8"

im trying this:

echo $this->private

but as expected php doesnt work just like that and is telling me this:

"Object of class stdClass could not be converted to string"

Can anyone point me in the right direction?

here is the code:

class Quiz { private $db; private $query; private $questions; private $answers; private $total; public function __construct(){ $this->db = new connection(); $this->db = $this->db->dbConnection(); } public function NumRows ($quiz) { $this->query = $this->db->prepare("Select count(*) FROM ".$quiz.""); $this->query->execute(); $this->total = $this->query->fetchObject(); var_dump($this->total); echo $this->total; } } $quiz = new Quiz(); $quiz->NumRows("01_ospf");

最满意答案

$this->private是一个对象,因此您需要访问该属性。 该属性名为count(*) 。 所以使用这个:

echo $this->private->{"count(*)"};

您应该为列添加别名,这样您就不必使用这种复杂的语法。 例如,如果你这样做:

SELECT COUNT(*) AS count ...

然后你可以写:

echo $this->private->count;

$this->private is an object, so you need to access the property. The property is named count(*). So use this:

echo $this->private->{"count(*)"};

You should give the column an alias so you don't have to use this convoluted syntax. E.g. if you do:

SELECT COUNT(*) AS count ...

then you can write:

echo $this->private->count;

更多推荐

本文发布于:2023-08-06 04:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   php   variable   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!