How to Create an Object Without Class in PHP ?
Last Updated :
24 Sep, 2024
In PHP, creating an object without a class refers to creating an instance of the stdClass, a built-in PHP class for creating generic objects. It allows developers to instantiate objects without explicitly defining a custom class, often used for dynamic data storage.
In this article, we will create an object without using a class in PHP.
Using new stdClass()
The new stdClass() approach in PHP allows you to create a generic object without defining a custom class. After creating an instance of stdClass, properties can be dynamically added, making it useful for temporary data storage or simple object structures.
Syntax:
// Creating an object
$object = new stdClass();
// Property added to the object
$object->property = 'Property_value';
Example : In this example we demonstrates the creation of an object using stdClass. It dynamically adds two properties, name and address, to the object, and then prints the object using print_r().
PHP
<?php
// Create an Object
$object = new stdClass();
// Added property to the object
$object->name = 'GeeksforGeeks';
$object->address = 'Noida';
// Print the object
print_r($object);
?>
OutputstdClass Object
(
[name] => GeeksforGeeks
[address] => Noida
)
Converting an Array to an Object
The Converting an Array to an Object approach in PHP involves typecasting an associative array into an object. Each key in the array becomes a property of the object, allowing dynamic access to the values through object notation.
Syntax:
// Declare an array
$arr = array(
key1 => val1,
key2 => val2,
...
);
// Typecast to convert an array into object
$obj = (object) $arr;
Example : In this example we converts an associative array into an object using typecasting. Each array key becomes an object property. The print_r() function is used to display the resulting object with its properties.
PHP
<?php
// Create an associative array
$studentMarks = array(
"Biology"=>95,
"Physics"=>90,
"Chemistry"=>96,
"English"=>93,
"Computer"=>98
);
// Use typecast to convert
// array into object
$obj = (object) $studentMarks;
// Print the object
print_r($obj);
?>
OutputstdClass Object
(
[Biology] => 95
[Physics] => 90
[Chemistry] => 96
[English] => 93
[Computer] => 98
)
Using json_decode with an Empty Object
The json_decode() function in PHP is used to decode a JSON string into a PHP object. By passing an empty JSON object ('{}'), you can create an object without a predefined class, and dynamically add properties as needed.
Example: In this example we use json_decode() to convert an empty JSON object into a PHP object. It dynamically adds properties property1 and property2, then displays the object using print_r().
PHP
<?php
$json = '{}'; // Empty JSON object
$obj = json_decode($json); // Convert JSON to PHP object
$obj->property1 = "value1"; // Dynamically add properties
$obj->property2 = "value2";
print_r($obj); // Print the object
?>
OutputstdClass Object
(
[property1] => value1
[property2] => value2
)
Similar Reads
How to load classes in PHP ? PHP load classes are used for declaring its object etc. in object oriented applications. PHP parser loads it automatically, if it is registered with spl_autoload_register() function. PHP parser gets the least chance to load class/interface before emitting an error. Syntax: spl_autoload_register(func
2 min read
Php Classes and Objects In PHP, Object-Oriented Programming (OOP) makes it easier to organize and reuse code. The two fundamental building blocks in OOP are classes and objects.PHP ClassesA class in PHP is a blueprint for creating objects. It defines the properties (variables) and methods (functions) that the objects creat
4 min read
What are magic methods and how to use them in PHP ? PHP magic methods are special methods that are called automatically when certain conditions are met. There are several magic methods in PHP. Every magic method follows certain rules - Every magic method starts with a double underscore ( __ ).They are predefined and neither can be created nor removed
4 min read
How to generate Json File in PHP ? In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation. Structure: {"data":[ { "sub_data1":"value1", "sub_data2":"value2","sub_dat
3 min read
How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a
2 min read
How will you access the reference to same object within the object in PHP ? In this article, we will see how we can access the reference to the same object within that object in PHP. To do that we have to use the "$this" keyword provided by PHP. PHP $this Keyword: It is used to refer to the current objectIt can only be used inside the internal methods of the class.$this key
2 min read
What is autoloading classes in PHP ? In order to use a class defined in another PHP script, we can incorporate it with include or require statements. However, PHP's autoloading feature does not need such explicit inclusion. Instead, when a class is used (for declaring its object etc.) PHP parser loads it automatically, if it is registe
2 min read
How to declare a function that receives one parameter in PHP ? In this article, we will know how to declare the function that receives the single parameter in PHP. Function calling is an important aspect of any programming language. Functions(methods) can be called within the script to simplify the performance of operations and eliminate the redundancy of writi
3 min read
PHP | get_class() Function The get_class() function is an inbuilt function in PHP which is used to return the class name of an object. Syntax: string get_class( object $object ) Parameters: This function accepts single parameter $object which holds the object that need to be tested. The value of this parameter can be omitted
1 min read
What are the __construct() and __destruct() methods in a PHP ? The constructor is the OOPs concept in PHP. It is a method that has the same name as the class name. It is defined inside the class and is used to automatically call when the object is created.PHP4 provides the constructor method whereas PHP5 provides the magic method __construct and __destruct. Thi
5 min read