PHP Objects Example | Object In PHP Tutorial
PHP Objects Example | Object In PHP Tutorial is today’s topic. PHP is an object-oriented language, although it does not have to be used as one since most PHP functions are not object-oriented. In object-oriented programming, the class is the definition of the object, whereas the object is an instance of an object, meaning that from one class, you can create many objects.
#Class in PHP
Class is the programmer-defined data type, which includes the 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 both data which are variables and functions that form the package called an: ‘object’. See the syntax of class in PHP.
<?php class App { }
We enclose a class using curly braces ( { } ) … just like you do with functions.
PHP Objects
The Object 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
We can create an object using the new operator.
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 as well as added a method called details which print 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. Notice that the Actor class has a constructor function, which is executed when an object is created.
A constructor receives the arguments which are later provided when constructing an object with a new keyword.
After we have constructed the object into a variable $millie, we can now use the object’s methods.
We implemented an object method details(), which prints out both of the 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 important points related to objects:
- Classes define how objects behave. Classes do not contain any data.
- Objects are the instances of classes, which contains the data.
- Members are variables that belong to the object.
- Methods are functions that belong to the object and have access to its members.
- The constructor is a specific method that is 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 function of the class which is automatically executed as the object of that class is created or instantiated.
The constructor is also called magic function because, in PHP, magic methods usually start with two underscore characters.
#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, so you’re able to do things like the following.
<?php class Actor {} $millie = new class extends Actor {}; var_dump($millie instanceof Actor);
Finally, PHP Objects Example | Object In PHP Tutorial is over.
Recommended Posts
PHP Array Intersect Example | PHP array_intersect() Function Tutorial
PHP Array Shift Example | PHP array_shift() Function Tutorial
PHP Array Values Example | PHP array_values() Function Tutorial