收到错误 - 已弃用:在 /home//public_html/srt.php 第 xx 行中已弃用动态属性 MyClass::$books 的创建。 第 xx 行是“$myClass->books = $books;”
我尝试在 MyClass 中声明 public $books ,但仍然遇到相同的错误
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Create connection
//$con=mysqli_connect("localhost","username","password","dbname");
$con=mysqli_connect("localhost","example","example","example");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM mytbl";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
$mydata = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
class MyClass {
}
class Book {
}
$books = $resultArray;
$myClass = new MyClass();
$myClass->books = $books;
$jsonData = json_encode($myClass);
echo $jsonData."\n";
}
// Close connections
mysqli_close($con);
?>
问题
$myClass->books = $books;
是指您正在为先前未声明的对象的数据成员赋值。您可以在这里阅读相关内容。
解决方案是简单地确保声明了稍后要引用的所有数据成员。在你的情况下,一个简单的解决方法是
class MyClass {
public $books;
}
在上面,声明了
$books
,所以稍后你可以做你的
$myClass->books = $books;
没有关于弃用的警告。