How to Create an Object in PHP

0
9648
PHP Objects Example | Object In PHP Tutorial

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.

Class in PHP

Class is the programmer-defined data type, including local methods and local variables. Class is the collection of objects. The object has properties and behavior.

Classes are the blueprints of objects.

One of the significant differences between the functions and classes is that a class contains data, variables, and functions that form the package called an: ‘object’.

See the syntax of the class in PHP.

<?php
 
class App {
 
}

We enclose a class using curly braces ( { } ) … just like you do with functions.

PHP Objects

The Object in PHP is an individual instance of a data structure defined by the class. We define the class once and then make many objects that belong to them. Objects are also known as instances.

Creating 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.

See the following code.

<?php

class Actor {

}

$millie = new Actor;
print_r($millie);

In the above code, we have defined the empty class Actor and then created an object $mille. See the output.

➜  pro php Actor.php
Actor Object
(
)
➜  pro

Now, let’s add constructor and method in the class.

<?php

class Actor {
  public function __construct($show, $character) {
    $this->show = $show;
    $this->character = $character;
  }

  public function details() {
    echo "Millie is as badass as " . $this->character . " in " . $this->show . ".\n";
  }
}

$millie = new Actor('Stranger Things', 'Eleven');
$millie->details();

In the above code, we have used parameterized constructor and added a method called details that prints the string in the PHP console.

See the output.

➜  pro php Actor.php
Millie is a badass as Eleven in Stranger Things.
➜  pro

Let’s analyze the complete code. First, notice that the Actor class has a constructor function executed when an object is created.

A constructor receives the arguments later provided when constructing an object with a new keyword.

After constructing the object into a variable $millie, we can now use the object’s methods.

We implemented an object method details(), which prints out both variables. Notice that the details() function does not receive any arguments, but it does have access to the $show and $character properties because they were previously defined in a constructor.

Here are some essential points related to objects:

  1. Classes define how objects behave. Classes do not contain any data.
  2. Objects are the instances of classes that include the data.
  3. Members are variables that belong to the object.
  4. Methods are functions that belong to the object and have access to its members.
  5. Finally, the constructor is a specific method executed when an object is created.

Constructors in PHP

A constructor is a key concept in object-oriented programming in PHP.

Constructor in PHP is a special type of class function that is automatically executed as the object of that class is created or instantiated.

The constructor is also called the magic function because magic methods usually start with two underscore characters in PHP.

Member Functions in PHP

After creating our objects, we can call the member functions related to that object.

A member function typically accesses members of the current object only.

Create an object without class in PHP

All objects in PHP have the class. A “default” class is stdClass, and you can create the objects of stdClass in the following way.

See the following code.

<?php

$obj = new stdClass();
$obj->name = 'Millie Bobby Brown';
print_r($obj);

See the output.

➜  pro php Actor.php
stdClass Object
(
    [name] => Millie Bobby Brown
)
➜  pro

In PHP 7, it is possible to create anonymous classes to do things like the following.

<?php

class Actor {}
$millie = new class extends Actor {};
var_dump($millie instanceof Actor);

That is it for creating an object in PHP.

Recommended Posts

PHP array_intersect()

PHP array_shift()

PHP array_values()

How To Convert PHP Object To Associative Array In PHP

PHP array_slice()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.