Signup/Sign In
OCTOBER 30, 2023

How to Define an Empty Object in PHP without creating a Class?

Technology #howto#object#php

    If you want to create an empty object in PHP with no property, method, etc. and you also don't want to create a new class for creating the empty object, then in this article I will share with you two standard ways to define an empty object in PHP, along with a few other non-standard hacks.

    PHP is an Object-oriented programming language which means you can create a class in PHP, use inheritance in PHP, create objects in PHP, etc. Usually, you would first create a class and then create objects for that class, but if you want to create an object without defining a class, you can do that too, using some standard pre-defined classes.

    PHP empty object

    Let's see how you can do this.

    Using the stdClass

    The stdClass is the default PHP object class. If you want to typecast a scalar type or array into an object then the object is of type stdClass. The stdClass in PHP has no properties, no methods, and it doesn't implement any Interface.

    If you want to create a generic object in PHP or an empty object, then you can use the stdClass.

    Here is the code:

    $x = new stdClass();

    That's it, all you have to do is use the new keyword along with the stdClass and your empty or blank object is ready.

    Using the above code, you can create an object in PHP without defining a new class. The object when created is empty, but you can add properties to the object in key-value form.

    Using Object Cast

    If you do not want to use the stdClass in your PHP code to create an empty object, then you can use the object casting style. This is similar to using the stdClass, and less confusing.

    If you use the stdClass to create an empty object in PHP, some of your teammates working on the same code might think that you have created some class stdClass and you are using it, or they may think that using the stdClass had some purpose, hence it can be confusing.

    Instead, the style that I have shared below is more obvious and anyone seeing the code immediately understands that some sort of empty object is being created.

    Here is the code:

    $oVal = (object)[];

    In the code above, we are typecasting an empty array into an object, hence internally the object of stdClass will be created because stdClass is the default class for object creation in PHP.

    or you can also use this code,

    $oVal = (object)NULL;

    In case you want to create an object with some properties, you can do it like this,

    $oVal = (object)[
    	'var1' => "something",
    	'var2' => "something else"
    ];
    echo $oVal->var1;


    something

    This style is also similar to how you create objects in JavaScript, therefore it would be easier to understand.

    PHP Empty Object

    One important point to note here is that in PHP you cannot create an empty object, or PHP will not treat the object as empty, even though it will be empty.

    If you run the following code:

    $obj1 = (object)null;
    var_dump(empty($obj1));
    
    $obj2 = new stdClass();
    var_dump(empty($obj2));


    bool(false)
    bool(false)

    As you can see in the code output above, both the code statements return false, hence for PHP the object is not empty.

    If you use the same code with an array, you will get true as output if the array is empty.

    $arr = array();
    var_dump(empty($arr));


    bool(true)

    So you must keep this in mind too.

    End Note

    So if you want to create an empty object in PHP to store some key-value pair style information, you can either use the stdClass or the (object) cast to do so. You don't have to create any class for this.

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS