Understanding Basic OOP Concepts in PHP: Part 3

Introduction

You should now know from my previous articles how to create a class and be familiar with properties, however if you have not yet, then please see my previous articles first:

  1. Understanding Basic OOP Concepts in PHP: Part 1
  2. Understanding Basic OOP Concepts in PHP: Part 2

After the completion of the previous articles, now this article explains methods. Whenever you start adding methods to classes they become quite powerful. An object subsequently becomes a very well encapsulated peice of functionality, with both data and methods to work on that data. Basically, a method is much like a function, except that it is tied to a specific class.

Method Visibility

In my previous article, you learned that a property can have one of three visibility levels (public, private and protected) and that is also true of methods. All methods may be called by simply some other method within the same class, if a method is declared as public, so it can be called outside of the class using class definitions, that potentially want to call these methods. If a method is decaled as a private method, only other methods within the same class can call it and at last a protected method can be called by other methods in the class, or by those methods of another class, that inherit this class.

How to create a Method

To create a method for a class, you can use the public, private or protected keyword followed by the method name and also followed by parentheses and write the code of the method within the curly braces.

class Example
{
public function MyMethod()
{
//code
}
}

It is not necessary to write public, private and protected keywords with method name because in PHP, by default it assumes public.

How to call Methods

For calling an object's method name, first you simply write the object's name then use the same arrow used for accessing properties (->) is used for methods, and then the name followed by the parenthesis.

$object->method();

Now I am using a simple example that creates a class with a method then we will create a object from the class and call the object's method.


Example


<?
php
class
Example
{

public
function MyMethod()
{

echo
"Welcome at c-charpcorner!.";
}
}
$
object=new Example();
$
object->MyMethod();
?>


Output

method-in-php.gif

Adding parameter and returning value of method

In the same manner that a function method also accepts a parameter or parameters or say arguments, a method can also return a value just like a function. You add parameters and return values with a very similar way like with functions. To add a parameter, specify the parameter name between the parentheses after the method's name.

public function MyMethod($parameter1, $parameter2)
{
//write code here
}

To return a value from a method, use the return keyword. Here I show you a simple example of how to use the return keyword with a method.

public function MyMethod($parameter1, $parameter2)
{
//write code here
return true;
}

Accessing object properties from method

Despite the fact that you are able to enjoyably pass values to and from a method applying parameters and return values, much of the energy of OOP is obtained when objects are as self-contained as possible. It means that an object's methods should ideally work mainly with the properties of the object, instead of depending on outside data to complete their task.

For accessing the object's property from within the method of the same object, you use the a variable named as $this, as follows:

$this->property;

Now I am writing an example for it.

Example

In the n following example, a class with the name of Example is created, with a single property name $name, and this class also contains a method named MyMethod(). The method displays the value of the $name property using echo via $this->name. After the class definition, create an object named $object, and call the object's MyMethod() to display the name.


<?
php
class
Example
{

public
$name="ABC";
public
function MyMethod()
{

echo
$this->name;
}
}
$object=new Example();
$object->MyMethod();

?>

Output

access-object-property.gif

As shown in the following code, you can also use $this to call an object's method from within another method of the same object.

Example

In the following example, the hi() method uses $this->GetName() to call the GetName() method in the same object for dispalying the stiring "Welome at c-charpcorner!."


<?
php
class
Example
{

public
function Getname()
{

return
"Welcome at c-charpcorner!.";
}

public
function Hi()
{

echo
$this->Getname();
}
}
$object=new Example;
$object->Hi();

?>

Output

call-an-object-to-another-way.gif

Static Methods

In the same manner as a property, PHP also provides the facility to create static methods. To create a method static, write the static keyword before the function keyword in the method definition.

class Example
{
public static function StaticMethod()
{
//write code here
}
}

Calling a static method

To call the static method, write the class name followed by two colons, followed by the method name.

For Example

Example: StaticMethod()

When you do not want to work with a class object, a static method is useful to add some functionality of that class.

Example

<?php

class Example

{

public static function add($parameter1,$parameter2)

{

return ($parameter1+$parameter2);

}

}

echo Example :: add(2,2);

?>


Output


static-method-in-php.gif

If you need to access a static method or property, or a class constant, from within a method of the same class, you can use the same syntax as you would outside the class.


Example

<?php

class Example

{

const myconst=456;

public static $staticvar=567;

public static function Method()

{

echo "Myconstant =". Example::myconst .","."</br>";

echo "StaticVariable=". Example::$staticvar .",";

}

}

$object=new Example();

$object->Method();

?>

Output

static-method1-in-php.gif


Similar Articles