New object in php

In PHP, you can create a new object using the new keyword followed by the name of the class. For example:

$obj = new MyClass();

This will create a new instance of the MyClass class and assign it to the variable $obj.

Here is an example of a simple class in PHP:

class MyClass {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function sayHello() {
        echo "Hello, my name is ". $this->name. " and I am ". $this->age. " years old.";
    }
}

You can then create a new instance of this class and call its methods like this:

$obj = new MyClass("John", 30);
$obj->sayHello(); // Output: Hello, my name is John and I am 30 years old.

You can also create a new object using the stdClass class, which is a built-in class in PHP that represents a generic object. For example:

$obj = new stdClass();
$obj->name = "John";
$obj->age = 30;

This will create a new object with two properties: name and age.

You can also use the json_decode function to create a new object from a JSON string. For example:

$json = '{"name":"John","age":30}';
$obj = json_decode($json);

This will create a new object with two properties: name and age.

It's worth noting that in PHP, objects are reference types, which means that when you assign an object to a new variable, you are not creating a copy of the object, but rather a new reference to the same object. For example:

$obj1 = new MyClass("John", 30);
$obj2 = $obj1;
$obj2->name = "Jane";
echo $obj1->name; // Output: Jane

This is because both $obj1 and $obj2 are references to the same object, and changing the value of one of them affects the other.