gpt4 book ai didi

php - 我想从 php 对象类添加自定义属性

转载 作者:行者123 更新时间:2023-11-29 17:22:11 29 4
gpt4 key购买 nike

我想为每个对象添加新属性。我的代码如下所示:

 function getCitationDataofquotes($var1){
$query = "SELECT `id`,`title`,`source`,`content`,`page`,`city`,`publisher` FROM `#__gpo_citations_quotes`";
$query .= " WHERE `id`='" . $var1 . "'";
if (!empty($query))
{
$some= array();
$this->_db->setQuery($query);
$data = $this->_db->loadObject();
return $data;

}
$value = $datapages->getCitationDataofquotes($var1);
$getdataofcitation['citations'][] = $value;

那么我的返回数据输出如下

{id: "12091",
title: "Homicide Victims by Year and Jurisdiction, 1989–90 to 2010–12",
source: "Homicide in Australia: 2010–11 to 2011–12: National Homicide Monitoring Program report",
content: " ACT = Australian Capital Territory]",
page: "p. 37",
city: "Canberra",
publisher: "Australian Institute of Criminology"
},

{
id: "8901",
title: "test",
source: null,
content: "",
page: null,
city: null,
publisher: null
},

现在我想为每个对象添加新属性。我想要每个对象的输出。

{ id: "8901",
title: "test",
source: null,
content: "",
page: null,
city: null,
publisher: null ,
**new property: something**}

最佳答案

首先,您的 $query 很容易受到 Sql 注入(inject)的攻击。顺便说一句,有多种方法可以实现您想要的结果。例如,您可以在查询中添加属性:

SELECT 'some value' As `some_field` ...

如果你想在 php 代码中添加它,你可以像上面 @madalinivascu 所说的那样动态添加它:

如果是一个对象:

$myObject->myNewProperty = 'My new value'; 
# if it is dynamically and you don't know the name ...
$myObject->{"myNewProperty"} = 'My new value';

如果是数组:

$myArray['myNewProperty'] = 'My new value';

关于php - 我想从 php 对象类添加自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191557/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com