Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an empty object in PHP

Tags:

object

php

with a new array I do this:

$aVal = array();  $aVal[key1][var1] = "something"; $aVal[key1][var2] = "something else"; 

Is there a similar syntax for an object

(object)$oVal = "";  $oVal->key1->var1 = "something"; $oVal->key1->var2 = "something else"; 
like image 239
ed209 Avatar asked Sep 16 '09 17:09

ed209


People also ask

How do you empty an object in PHP?

An object is an instance of a class. Using the PHP unset() function, we can delete an object. So with the PHP unset() function, putting the object that we want to delete as the parameter to this function, we can delete this object.

How do I declare an object in PHP?

To create an Object in PHP, use the new operator to instantiate a class. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.

Is null or empty PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

Is Empty function in PHP?

PHP empty() FunctionThe empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.


1 Answers

$x = new stdClass(); 

A comment in the manual sums it up best:

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces.

When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance.

like image 85
zombat Avatar answered Oct 01 '22 05:10

zombat