Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to add data to an object in php

//this is my code 

lets say that we have an object with the name ($obj), 
and we want to add Properties to it, we can do many ways and some of them are:-
  
1- we can change the type of object into array using casting,
then we add elemnt to the array then we change it back to an object, like this:-
  
<?php
  $obj = (array)$obj;
        $obj['car'] = '1234';
        $obj['ball'] = 'hello';
        $obj = (object)$obj;
  var_dump($obj);
?>

then the object $obj will have new two Properties which are:-
the first one with the name car and value of '1234',
the second one with the name ball and value of 'hello'
  
2- this is a second way to do it:-
  
<?php
$obj->car = 'value1';
$obj->ball = 'value2';
var_dump($obj);
?>

then the object $obj will have new two Properties which are:-
the first one with the name car and value of 'value1',
the second one with the name ball and value of 'value2'
  
  
3- if you want to add Properties to an object but also delete or remove,
all the previous Properties of the object, then we do this:-
  
<?php
  $obj = (array)null;
        $obj['attributes'] = '1234';
        $obj['attributes'] = 'hello';
        $obj = (object)$obj;
  var_dump($obj);
?>
Comment

how to add data to an object in php

//this is my code 

lets say that we have an object with the name ($obj), 
and we want to add Properties to it, we can do many ways and some of them are:-
  
1- we can change the type of object into array using casting,
then we add elemnt to the array then we change it back to an object, like this:-
  
<?php
  $obj = (array)$obj;
        $obj['car'] = '1234';
        $obj['ball'] = 'hello';
        $obj = (object)$obj;
  var_dump($obj);
?>

then the object $obj will have new two Properties which are:-
the first one with the name car and value of '1234',
the second one with the name ball and value of 'hello'
  
2- this is a second way to do it:-
  
<?php
$obj->car = 'value1';
$obj->ball = 'value2';
var_dump($obj);
?>

then the object $obj will have new two Properties which are:-
the first one with the name car and value of 'value1',
the second one with the name ball and value of 'value2'
  
  
3- if you want to add Properties to an object but also delete or remove,
all the previous Properties of the object, then we do this:-
  
<?php
  $obj = (array)null;
        $obj['attributes'] = '1234';
        $obj['attributes'] = 'hello';
        $obj = (object)$obj;
  var_dump($obj);
?>
Comment

PREVIOUS NEXT
Code Example
Php :: php number_format 
Php :: wordpress thumbnail 
Php :: delete record using laravel 
Php :: php convert to lowercase 
Php :: separate date from datetime php 
Php :: php heredoc 
Php :: how to get current url in laravel 
Php :: minus 1 year php 
Php :: php sort reverse 
Php :: laravel check if model relation exists 
Php :: php setinterval 
Php :: how to use multiple permission in blade 
Php :: eloquent delete all where 
Php :: php cli display errors 
Php :: permissions for laravel deployment 
Php :: increase memory limit wordpress 
Php :: current url wordpress 
Php :: E: Unable to locate package php8.0 
Php :: how send user to 404 page if not exist page in laravel 
Php :: php rsa encryption 
Php :: Installation request for phpoffice/phpspreadsheet 1.4.0 - satisfiable by phpoffice/phpspreadsheet[1.4.0] 
Php :: php connect to mysql 
Php :: continue not in the loop or switch 
Php :: php list directories 
Php :: IlluminateDatabaseEloquentCollection to array 
Php :: laravel create text file 
Php :: php remove all whitespace from a string 
Php :: delete mysql php 
Php :: create a modal livewire laravel 
Php :: get last month using php 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =