PHP Class Object
As discussed in the last tutorial, a class is nothing but a blueprint or a user defined datatype which can be used by creating its objects.
Class is a logical entity while its object is real.
A class generally includes local variables and local methods.
And an object is an instance of the class which holds the local variables with values assigned and using the object we can call the local methods defined in the class.
In this tutorial, we will learn how to initialize an object of any class in PHP.
Create an Object of a Class in PHP
To create an object of a class we use the new
keyword.
Let's take a quick example where we will be creating object for a simple PHP class.
<?php
class Studytonight {
// to store name of person
var $url = "studytonight.com";
// simple class method
function desc() {
echo "Studytonight helps you learn Coding.";
}
}
// creating class object
$obj = new Studytonight();
?>
As you can see, we used the new
keyword in the last line to create an object of the class Studytonight and assigned it to the variable $obj
.
Once we have an object of any class, we can use it to access the class methods and variable using ->
operator.
<?php
class Studytonight {
// variable with a default value
var $url = "studytonight.com";
// simple class method
function desc() {
echo "Studytonight helps you learn Coding.";
}
}
// creating class object
$obj = new Studytonight();
// accessing class variable
echo $obj->url . "<br/>";
// calling class method
$obj->desc();
?>
studytonight.com
Studytonight helps you learn Coding.
Accessing Class Variables and Methods
To access class variable and methods using the object of the class, we use the ->
operator followed by the name of the variable or the method's name.
We have already showcased this in the example above, but let's have another example, with a little more complex class.
<?php
class Person {
// first name of person
var $fname;
// last name of person
var $lname;
function showName() {
echo "My name is: " . $this->fname . " " . $this->lname;
}
}
// creating class object
$john = new Person();
// assigning values to variables
$john->fname = "John";
$john->lname = "Wick";
// calling the method function
$john->showName();
?>
My name is: John Wick
In the above class, inside the showName()
method we have used $this
keyword, it is used to refer the calling object. We will learn more about it in upcoming tutorials.
Similarly we can create multiple objects of a class with different value assigned to the local class variables. Let's do that,
<?php
class Person {
// first name of person
var $fname;
// last name of person
var $lname;
function showName() {
echo "My name is: " . $this->fname . " " . $this->lname;
}
}
// creating class object
$john = new Person();
// assigning values to variables
$john->fname = "John";
$john->lname = "Wick";
// calling the method function
$john->showName();
echo "<br/>";
// creating class object
$james = new Person();
// assigning values to variables
$james->fname = "James";
$james->lname = "Bond";
// calling the method function
$james->showName();
?>
My name is: John Wick
My name is: James Bond
The class variables fname
and lname
, both are available as separate copies in the objects $john
and $james
, where we can assign values to them and then use them.