Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Declare and Initialize PHP Objects Without Class?

How To Declare and Initialize PHP Objects Without Class?


In this article, we go over how to initialize an object in PHP without Class. Initializing an object means that we give an object that has been created from a class a value or values.

For example, say we create an object named car from the class of the vehicle. The vehicles class represents a range of vehicles and a car is just a particular object or instance of the class of this vehicle. We can then assign values to the car such as the car’s color, the car’s cost, the car’s engine, etc. If we set the car’s color to red, we have initialized the object with this color property.

So initializing an object is just giving values to the object, that represent the object’s properties. Using new stdClass() to create an object without class: For creating an object without a class, we will use a new stdClass() operator and then add some properties to them.

PHP is an object-oriented language, although it does not have to be used since most PHP functions are not object-oriented. In object-oriented programming, the class is the object’s definition, whereas the object is an instance of an object, meaning that you can create many objects from one class.

Table of Contents

Example # 01:

$myobject = new STDClass();
$myobject->member1 = "hello, I'm 1";
$myobject->member1o = new stdClass;
$myobject->member1o ->member1 = "hello, I'm 1.1";
$myobject->member2 = "hello, I'm 2";

Example # 02:

$myobject = (object)array(
     'member1' => "hello, I'm 1",
     'member1o' => (object)array(
         'member1' => "hello, I'm 1o.1",
     'member2' => "hello, I'm 2",

Example # 03:

$myobject = (object)[
     'member1' => "hello, I'm 1",
     'member1o' => (object)['member1' => "hello, I'm 1o.1"],
     'member2' => "hello, I'm 2",

Example # 04:

$myobject = (object) array("name" => "member 1", array("name" => "member 1.1") );

Example # 05:

      // Create an associative array
      $studentMarks = array(
      // Use typecast to convert the array into an object
      $myobject = (object) $studentMarks;

You Can Convert An Object Into Associative Array Too:

$associatoveArrayFromObject= array($myobject );


No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
How To Open A DIV Content/Data Into A POPUP Window?

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *