learn

Object Oriented Programming

Created by: Dr.Ferrous

Object Oriented Programming

Created by: Dr.Ferrous
twitter google+ facebook pin 

Object Oriented Programming (OOP) is a programming concept that allows programmer to group similar tasks into classes. It organizes the data and structure of an application. Here in this tutorial, you will learn about basic OOP concept and also how data and functions can be represented as reusable objects that are cut down on code and time.

Object

an object is a living instance of a class. This means that an object is created from the definition of the class and is loaded in memory.

A good example to understand this is to compare objects with humans – and understand that all of us are objects. If God wants to send a human to earth, what is easy for Him to do? Create and define properties and attributes of each human separately or create a one time template and generate objects out if it. Therefore, this onetime template is a Class and you, I & everyone in this world is an object – that is a living instance of class Human.

 

Creating Objects in PHP5 Class:


In PHP to create an object of a class we use the keyword “new“. Below is the syntax style of how to create objects in PHP:

In the above syntax style, $obj_name is a variable in PHP. “new” is the keyword which is responsible for creating a new instance of ClassName.

Example:

In the above example $cust1 and $cust2 are two objects of the Customer Class. Both these objects are allocated different blocks in the memory.

Class

”A class is user defined data type that contains attributes or data members and methods which work on the data members”

For creating a class,  use the keyword “class” followed by the name of the class. The name of the class should be meaningful to exist within the system. The body of the class is placed between two curly brackets within which you declare class data members/variables and class methods.

 

Syntax of a PHP class structure:

 

Example of a Class:

In the above example program, Customer is the name of the class and $first_name and $last_name are attributes or data members. setData() and printData() are methods of a class.

 

Naming Convention for Class:
As a general Object Oriented Programming practice, it is better to name the class as the name of the real world entity rather than giving it a fictitious name. For example, to represent a customer in your OOAD model; you should name the class as Customer instead of Person or something else.

Naming Conventions for Methods:
The same rule applies for class methods. The name of the method should tell you what action or functionality it will perform. E.g. getData() tells you that it will accept some data as input and printData() tells you that it will printData(). So ask yourself what you would name a method which stores data in the database. If you said storeDataInDB() you were right.

Look at the manner in which the method storeDataInDB() has been named. The first character of the first word of the method is lower case i.e. ‘s‘tore. For rest of the words in the function have been named as first character Uppercase and the remaining characters of the words as lower case i.e. ‘D’ata . Therefore it becomes storeDataInDB()

Naming Convention for Attributes:
Attributes of a class should be named on the basis of the data that they hold. You should always give meaningful names to your attributes. For attributes you should split the words with an underscore (_) i.e. $first_name, $last_name, etc.

Class Attribute

Defining an attribute is as easy as declaring a variable within the body of the class. At the time of declaring a data member/variable within the body of the class, it is also possible to assign a default value to the attribute or data member.

Attributes can either be public, private or protected – the default being public. These are called Access Specifiers. We will learn more about access specifiers in this PHP OOP tutorials chapter Access Specifiers.

Example:

In the above example $first_name, $last_name and $outstanding_amount are data members or attributes of the class Customer. Of these attributes, $outstanding_amount has a default value of zero assigned to it. This means that when an object of the Customer class has been created, $first_name and $last_name will be blank whereas; $outstanding_amount will be initialized with default value of zero.

Let’s take a problem statement and define a PHP Class for it.

Define a class to encapsulate Student. The data required to be captured is Student’s first name, last name, date of birth, address and telephone number.

Solution:

In the above example $first_name, $last_name, $date_of_birth, $address and $telephone are all data members of class Student. These data members define the data that one Student Object will store. Therefore, you can create two objects of a Student class having different data as follows:

In the above example, we create two objects of the Student class $s1 and $s2. Both these objects have their own memory space within which they store their respective data. Because each data member is public you can directly access the data members of the Student Class using the arrow operator (reference operator) i.e. $s1->first_name =”Steve”; You will learn more about public, private and protected in the PHP tutorials chapter Access Specifiers.

Objects as Attributes
In addition to declaring attributes as intrinsic data types (int, string, etc), you can also declare data members as objects of another class. This is called aggregation in Object Oriented Analysis and Design (OOAD). Lets look at an example below:

In the above example setCust() method accepts a Customer type of parameter which is stored internally in the $customer data member.

The advantage of the above method is that it allows you to change the customer object independently of the order object. Imagine having to add new data members to the Customer object. You only have to modify the Customer object without having the need to modify the Order Object.

Class Method

A class method or functions is the behavior or functionality of a class i.e. they provide the necessary code for the class in which it is defined. Examples could be a saveCustomer() method in the class Customer or a printDocument() in the Document class.


Methods perform operations on the data members of the class and can be declared as private or public. A class method is exactly similar to PHP functions, it’s just that class functions are declared inside classes and accessed using the -> (arrow operator / dereferencing operator).

Methods can also be declared as either public, protected or private.

Lets look at an example to understand PHP Class methods better:

In the above example setName() is the class method of the Customer class. The setName() class method is responsible for accepting the name of the customer and storing it in the internal data member i.e. $name.

The reason why you require methods is so that you can perform necessary validations on the data passed. Let’s re-look at the above example with necessary validation code.

In the above example the setName() method accepts a customer’s name and validates to check if $name is blank. If $name is blank the setName() function returns false; otherwise it stores the $name in the $this->name of the class and returns true.

What is Accessor and Mutator methods


1. Accessor Methods:

Accessor methods are also know as getter methods. The reason why we need an accessor method is to be able to read the value of a property/attribute in a class object. In real OOAD practice most of the data members that you define would either be private or protected, therefore to access data of such data members that have been defined as either private or protected will require an implementation of accessor or getter methods.

2. Mutator Methods:

Mutator methods are opposite to accessor methods. Mutator methods provides a mechanism to store data in data members that have either been declared as private or protected. The reason why you should provide a mutator method is to provide necessary validation on the data that is to be stored in the data member of the class.
Note: To make a property or data member as read only; you should not provide a setter or mutator method.

Lets look at an example of accessor and mutator methods below:

In the above example the setName() method accepts a customer’s name and validates to check if $name is blank. If $name is blank the setName() function returns false; otherwise it stores the $name in the $this->name of the class and returns true. The getName() returns the name stored in the $name data member of the $c1 object.

Constructor

A constructor is a special function of a class that is automatically executed whenever an object of a class gets instantiated.


What does this all mean?
A constructor is a special function – this means that a constructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is created.

Why do we need a Constructor?
It is needed as it provides an opportunity for doing necessary setup operations like initializing class variables, opening database connections or socket connections, etc. In simple terms, it is needed to setup the object before it can be used.

PHP5 Constructor
In PHP5 a constructor is defined by implementing the __construct() method. This naming style has been introduced in PHP5. In PHP4, the name of the constructor was the same name as that of the class. So, for example if you had a class Customer, you would have to implement a function Customer().

PHP5 to be backward complaint also supports the PHP4 rule. When an object is created, PHP5 searches for __construct() first. If __construct() is not defined it then searches for a method with the same that of the class. However, if you define both; PHP5 will first search for __construct() method and execute it if available, otherwise it will execute the same class name function.

Let’s look at how to define a PHP5 Constructor:

 

Example:

In the above example, we create a new object of the Customer class. the ‘new’ operator is responsible for creating the Customer class. At this point PHP5 searches the Customer class to see if a constructor has been defined. Therefore, it calls the constructor method i.e. __construct() defined. The __construct() method sets the $first_name and $last_name to blank and sets the $outstanding_amount to zero.

Parameterized Constructor or Argument Constructor

A parameterized or argument constructor is a constructor which accepts values in the form of arguments in the constructor. Unlike other programming languages where overloaded argument constructors is possible, in PHP5 you cannot overload constructors.

Example:

In the above example, we create a new object $c1 and pass values “Stuart”, “Broad” and zero to the constructor. The constructor now takes 3 arguments and stores them in the internal private variable $first_name, $last_name and $outstanding_amount respectively.

Destructor

A destructor is a special function of a class that is automatically executed whenever an object of a class is destroyed.


What does this all mean?
A destructor is a special function – this means that a destructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is destroyed. An object of a class is destroyed when

  • it goes out of scope
  • when you specifically set it to null
  • when you unset it or when the program execution is over.

Why do we need a Destructor?
It is needed as it provides an opportunity for doing necessary cleanup operations like unsetting internal class objects, closing database connections or socket connections, etc. In simple terms, it is needed to cleanup the object before it is destroyed. You should also read more about Garbage Collection to understand how clean up happens.

PHP5 Destructor
A PHP5 destructor is defined by implementing the __destruct() method. In PHP4 however, the concept of a destructor did not exist.

A destructor cannot take any arguments.

Let’s look at how to define a PHP5 Destructor:

 

 

Example:

In the above example, we create a new object of the Order class. The argument constructor of the Order class takes two parameters i.e. $order_id and $customer object. After the program completes its execution, the object goes out of scope because the program stops execution and hence the destructor is automatically called.

Access Specifiers

Access specifiers specify the level of access that the outside world have on the class methods and class data members. Access specifiers can either be public, private or protected.


Why do we need Access specifiers
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.

Explanation of each access specifier

  1. Private
  2. Protected
  3. Public


1. Private
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:

In the above example, echo $c->name will give you an error as $name in class Customer has been declared private and hence only be accessed by its member functions internally. Therefore, the following line echo $c->getName() will display the name.

 

2. Public
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:

In the above example, echo $c->name will work as it has been declared as public and hence can be accessed by class member functions and the rest of the script.

3. Protected
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:

In the above example, echo $dc->name will not work work $name has been defined as a protected variable and hence it is only available in Customer and DiscountCustomer class.

Important Note of Access Specifier in PHP5
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.

$this variable

$this variable is a pointer to the object making the function call. $this variable is not available in static methods.


Example:

In the above example, $c1 and $c2 are two separate Customer objects. Each object has its own memory to store names. But if you see the function setName() is common. During run time, how can it make a difference as to which memory location to use to store the function values. Therefore, it uses the $this variable. As mentioned earlier, $this variable is a pointer to the object making a function call. Therefore when we execute $c1->setName(“Stuart”), $this in the setName() function is a pointer or a reference to the $c1 variable.

instanceOf Operator

PHP5 introduces a new operator by the name of instanceOf. instanceOf is used to check if two objects passed as operands belong to the same class. This check can also happen when an object is compared with a class name.


In PHP4 a similar functionality existed with a method is_a(), which has been replaced by the instanceOf operator in PHP5.

Lets look at an example below:

In the above example, we are comparing to check if $p1 and $p2 belong to the same class i.e. Student. In this case $p1 and $p2 both belong to the same class Student and hence the output is True.

You can also use instanceOf operator to compare an object with the name of the class, look at the example below:

In the above example, $p1 object is being compared to check if its a Student type of object. In this case $p1 is a Student type of object and hence the output is True.

Behaviour of instanceOf operator in inheritance

In the above example, $c1 child class object is being compared with $p1 which is a parent class object. This is possible because Student class is a child of the Teacher Class, just that the Student class is a specialized form of the Teacher class and therefore this becomes possible. However the reverse is not possible, we cannot compare if($p1 instanceof $c1) and will result in an error.

Class Constants

In PHP4 the only constants that we would declare were global constants. In PHP5, it is possible to define a class level constant. These constants are specific to the class and hence don’t clutter the global level constant space.


To declare a constant in a class, PHP5 provides developers with a new keyword i.e. const; look at the example below:

In the above example, const is a keyword and TYPES is the name of the constant. Outside the class definition we echo the value of the constant by using the scope resolution operator (::) like this Customer::TYPES. Observe that we don’t need to create an object of the class to make use of the constant.

The next logical question is if we can create an object of the Customer class and using the scope resolution operator access the constant. The answer is no; reason – because a constant belongs to the class definition scope and not to an object.

Example of accessing Constants within a function

Few important points on Constants

  • Variables defined as constants cannot be changed.
  • Only a string or numeric value can be assigned to a constant.
  • Arrays, Objects & Expressions cannot be assigned to a constant.
  • A class constant can only be accessed via the scope resolution operator (::) executed on the class name.
  • A class constant cannot have access specifiers assigned to it (private, public & protected)

Static Data Members and Methods

A data member that is commonly available to all objects of a class is called a static member. Unlike regular data members, static members share the memory space between all objects of the same class.


Meaning of static methods

A static method is a class method that can be called without creating an instance of a class. Such methods are useful when creating utility classes.

Defining static data members in PHP5

To define a static member in PHP5 you need to prefix the class member name with the keyword ‘static’. Look at the example below.

In the above example $instance_count is declared as a static data member

 

Defining static methods in PHP5

To define a static data methods in PHP5 you need to prefix the class method name with the keyword ‘static’. Look at the example below.

In the above example getInstanceCount is declared as a static method

 

Accessing static data members in PHP5

A static member data can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class

Look at the example below:

In the above example, $instance_count is a static data member. Every time a new object is created the constructor is executed and the $instance_count variable is incremented by one. To echo the value contained in $instance_count variable, we use the :: (scope resolution) operator.

 

Accessing static method in PHP5

A static method can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don't need to create an instance of that class. However, you can also access it with an instance variable.

Look at the example below:

 

Rules to keep in mind for static methods

  • A static method can only access static data members
  • A static method does not have access to the $this variable
  • Please feel free to write back for any clarification.

Final Class and Final Method

A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Below is an example to define final class and final method:

In the above example, BaseClass is declared as final and hence cannot be extended (inherited). DerivedClass tries to extend from BaseClass and hence the compiler will throw a compile error.

 

Meaning of Final Method


A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the ‘final’ keyword. Example below:

In the above example, DerivedClass extends from BaseClass. BaseClass has the method myMethod() declared as final and this cannot be overridden. In this case the compiler causes a compile error.

When to declare a class as final
You should declare a class as final when you think that you implementation of that class should not change in the derived class. You should do this mainly for Utility classes where you don’t want the behavior/implementation of your class to change.

When to declare a method as final
You should declare a class method as final when you think that the method you develop contains necessary functionality to support your application and any modification or change to the functionality can cause unexpected errors/bugs.

Exception

An exception is a logical/system error that occurs during the normal execution of a script. The exception could either be raised by the system or the program itself it the exception cannot be handled and the caller script/function needs to be informed about the same.


The use of a try…catch block
PHP5 introduces the try…catch block to trap exceptions. Look at the example below.

In the above example, the method check() is called between the try {} block. The try{} block is the area where you will place your code that could raise an exception. Below the try{} block is the catch() {} block. The catch block expects the Exception type of object as a parameter. Within the catch() {} block you will place your logic to either fix the issue or log the error.

In the function check(), we raise an Exception using the ‘throw’ keyword. The statement following ‘throw’ is the syntax of creating a new object of Exception type. The exception class accepts two parameters. The left parameter is a string that is the error message and the right parameter is the integer error code that you wish to assign to that error.

Extending the Exception class

You can also extend the exception class as follows:

All un-handled exceptions are passed up the function stack order till the time either a try…catch block is not made available. If a try…catch block is unavailable it is passed to the PHP core and the program execution stops there.

Inheritance

Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class or child class to inherit the attributes and behaviors of a base-class or parent class. These inherited attributes and behaviors are usually modified by means of extension.


PHP Inheritance
To inherit in PHP, you should use the keyword ‘extends’ in the class definition. In PHP only single inheritance is allowed. Look at the example below:

In the above example, class Customer extends from the Person class. This means that Customer class is the child class and the Person base class. The child class Customer extends the method getName() and calls it in the getCustomerName() method of the Customer class.

 

Inheritance and Access Specifiers
You can refer to the Access specifiers tutorial to understand what it means. Let’s understand the use of Access Specifiers with context to Inheritance.

Access specifiers specify the level of access that the outside world (other objects and functions) have on the data members or member functions of the class. Please note that all data members and member functions are public by default.

Lets look at how three access specifiers viz. private, public and protected behave in Inheritance:

1. private
A private access specifier is used to hide data and member functions. A method or data member declared as private can only be accessed by the class itself and neither the outside program nor the derived class can have access to it. Lets look at an example below:

causes an error as we are trying to access $name that has been declared as a private member variable. We can however access the $age data member without any limitation as its public. We will learn more about public later in this tutorial.

The reason why data members are declared private is to avoid the outside programs to either accidently modify the values without necessary validation. If you make a data member private you should provide accessor/mutator methods to access. Read more about accessor/mutator methods here (You will have to scroll a bit down and reach to “Notes on Accessor and Mutator methods”)

Note: When you define a method as private that method can only be called from within that class and not from outside that is the global level script.

 

2. public
A public access specifier allows the data members and methods to be access from anywhere in the script. Lets look at an example below:

prints 30 on the screen as $age is a public variable and hence can be accessed from anywhere in the script. Please note that if you declare any data member or method without a access specifier it is considered as ‘public’.

 

3. protected
A protected access specifier allows the derived class to access the data member or member functions of the base class, whereas disallows global access to other objects and functions.

in the setName() function is referring to the $name data member of the Person class. This access is only possible because the $name variable has been declared as protected. Had this been private; the above statement would have raised an error. Further, in the statement towards the end;

$c1->name = “Stuart”;

raises an error as $name in the Person class has been declared as protected and not public.

 

PHP5 Inheritance – Method Overriding
Lets learn how to override methods in PHP, but before we understand how to override methods in PHP; lets define method overriding.

Definition of Method Overriding:
Method overriding is when the function of base class is re-defined with the same name, function signature and access specifier (either public or protected) of the derived class.

The reason to override method is to provide additional functionality over and above what has been defined in the base class. Imagine that you have a class by the name of Bird from which you derive two child classes viz. Eagle and Swift. The Bird class has methods defined to eat, fly, etc, but each of the specialized classes viz Eagle and Swift will have its own style of flying and hence would need to override the flying functionality.

Lets look at an example with Bird:

In the above example, we create two objects of class Eagle and Swift. Each of these classes have overridden the method fly() and have provided their own implementation of the fly() method that has been extended from the Bird class. The manner in which they have been extended the Bird class fly() method is not called as both these classes have provided a new functionality for the fly() method.

 

PHP Inheritance – Invoking parent methods
When you override a method of the base class, it’s functionality is completely hidden unless it has been explicitly invoked from the child class. To invoke a parent class method you should use the keyword parent followed by the scope resolution operator followed by the name of the method as mentioned below:

parent::function_name();

Look at the example below:

In the above example, look at the way in which the showData() function in the Customer child class is invoking the the Person parent class’s showData() function. When the program executes the showData() method if the Customer class is called which inturn calls the showData() function of the parent class. After the parent class’s showData() function complets its execution the remaining code in showData() function of the Customer class is executed.

 

PHP Inheritance – Invoking parent Constructor and Destructor
We can get the parent PHP5 constructor and PHP5 Destructor to be invoked in the same way as invoking the parent method, refer to the example below:

Abstract Class and interface

An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement. The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract.


Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created

To define a class as Abstract, the keyword abstract is to be used e.g. abstract class ClassName { }

Example of Abstract Class:

In the above example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the responsibility of the child class to provide the functionality of getPrice(). The BookShelf class is a child of the Furniture class and hence provides the function body for getPrice().

 

Private methods cannot be abstract
If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.

 

What is an Interface?
An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour.

To extend from an Interface, keyword implements is used.

We can have a class extend from more than one Interface.

In the above example, Document and the Indexer class are two independant classes. The Indexer class is designed to index the contents of any text. Using the Storable interface above, we declare a method getContentsAsText() in the Document class. Because the Indexer class is only concerned with the TEXT, hence we can call getContentsAsText() method on the object of Document. This way any class if it implements the method getContentsAsText() can get indexed

 

Difference between Abstract Class and Interface


Abstract Classes
An abstract class can provide some functionality and leave the rest for derived class
The derived class may or may not override the concrete functions defined in base class
The child class extended from an abstract class should logically be related

Interface
An interface cannot contain any functionality. It only contains definitions of the methods
The derived class must provide code for all the methods defined in the interface
Completely different and non-related classes can be logically be grouped together using an interface

Polymorphism

Polymorphism is derived from two Greek words. Poly (meaning many) and morph (meaning forms). Polymorphism means many forms. In C you have two methods with the same name that have different function signatures and hence by passing the correct function signature you can invoke the correct method.


This is how polymorphism is achieved in languages like C where in a function sum(int, int) differs from sum(float, float). Therefore the method sum() has many forms depending on the parameters being passed to it.

When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism

Why method polymorphism cannot be achieved
The reason why polymorphism for methods is not possible in PHP is because you can have a method that accepts two parameters and call it by passing three parameters. This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.

Because PHP is not type strict and allows variable arguments, this is why method polymorphism is not possible.

Polymorphism in PHP
Since PHP introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.

Lets look at an example:

In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BassClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.

Overloading and Overriding

Overloading and Overriding are forms of polymorphism in OOP.  According to Object Oriented Programming (OOP) concept if a class has methods of the same name but different parameters then we say that we are overloading that method. Also if we were to create a method in the child class having the same name, same number of parameters and the same access specifier as in its parent then we can say that we are doing method overriding.


As we know that PHP is not type strict, means if we implement overloading in C++ or Java, the function will look like add(int,float) is different from add(float,int) or even add(float,int,int) but it is not possible in PHP. Actually polymorphism is not easy in PHP. In this post, you will learn how to handle polymorphism in PHP.

1. Method Overloading
As discussed above PHP does not support method overloading compared to other language like Java or C++. For example:

if above example code convert to Java or C++, it will work without any errors. But if we run above code, it will throw error the error “Cannot redeclare ABC::displayMessage()”. Simple overloading is not supported by PHP. But you can implement overloading by using the PHP magic method __call().

Example:

__call() function is triggered when invoking inaccessible methods in an object context. The syntax for __call() is mixed __call(string $name,array $arguments).
The $name parameter is the name of the method being called. The $arguments parameter is an enumerated array containing parameters passed to the $name method.

2. Method Overriding
If you create a method in the child class having the same name, same number of parameters and the same access specifier as in it’s parent then we can say that we are doing method overriding in PHP. for example:

As according to polymorphism, if we invoke a method by inspecting the object at run time is a case of polymorphism. Here in above example either we call method output () of a class AA or class BB is depend on object which is making the call to the method.