I have the following code running on PHP 7.4.2, but after the update to PHP 8.1.2, I'm getting an error in order to fill an array with some data.
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myObj->status = '1';
$myObj->video = $row["token"];
$title = str_replace(".mp4","",$row["filename"]);
$myObj->title = $title;
I'm getting the following error:
Fatal error: Uncaught Error: Attempt to assign property "status" on null
Is this problem related to some change in PHP 8? I've tried to find some answers but have no success on how I can update the code to run in this new scenario.
CodePudding user response:
You need to create $myObj
before you can assign to $myObj->status
.
while($row = $result->fetch_assoc()) {
$myObj = new StdClass;
$myObj->status = '1';
$myObj->video = $row["token"];
$title = str_replace(".mp4","",$row["filename"]);
$myObj->title = $title;
CodePudding user response:
You are using a object of class without declaring it before which means that you need to tell PHP by declaring it outside the while loop.
class YourClassName{
public $status;
public $video;
public $title;
}
$myObj = new YourClassName;
while($row = $result->fetch_assoc()) {
$myObj = new StdClass;
$myObj->status = '1';
$myObj->video = $row["token"];
$title = str_replace(".mp4","",$row["filename"]);
$myObj->title = $title;
}