I am continuing the post series about the concepts of OOP in PHP5 from basics to very advanced level. We have learned about the class basics and public class properties in our previous tutorials. Today i am going to write about the basics of class methods.

Class properties allow your objects to store data and class methods allow your objects to perform tasks

What are Class Methods

Methods are special functions declared within a class. As you might expect, a method declaration resembles a function declaration. The function keyword precedes a method name, followed by an optional list of argument variables in parentheses. The method body is enclosed by braces,

public function sample_method( $argument1, $argument2 ) {
// Method Logic
}

Class Methods Scope

  • Methods must be declared in the body of a class.
  • Methods can accept a number of qualifiers, including a visibility keyword.
  • Like properties, methods can be declared public, protected, or private.
  • By declaring a method public, you ensure that it can be invoked from outside of the current object.

If you omit the visibility keyword in your method declaration, the method will be declared public implicitly

Class Example

I am continuing my previous example of class Product. Today we will set and get class properties via class method.

I will recommend to set class properties via class methods because, There are a number of problems to setting property values without methods, as PHP lets you set properties dynamically, you will not get warned if you misspell or forget a property name.

/**
* Class Tutorial
*/

class Product {

	/**
	* Class Properties
	*/

	public $productTitle = "Product Title";
	public $productName = "Product Name";
	public $productPrice = 0;

	/**
	* Class Methods
	*/

	/**
	* Set Product Title
	*/

	public function setProductTitle( $arg ) {
		$this->productTitle = $arg;
	}

	/**
	* Set Product Name
	*/

	public function setProductName( $arg ) {
		$this->productName = $arg;
	}

	/**
	* Set Product Price
	*/

	public function setProductPrice( $arg ) {
		$this->productPrice = $arg;
	}

	/**
	* Get Product
	*/

	public function getProduct() {
		$temp = "Title: " . $this->productTitle . " <br />";
		$temp .= "Name: " . $this->productName . " <br />";
		$temp .= "Price: " . $this->productPrice . " <br />";
		return $temp;
	}

}

/**
* Class Object
*/

$product1 = new Product();

/**
* Set Object Properties
*/

$product1->setProductTitle( "Smart Phone" );
$product1->setProductName( "iPhone" );
$product1->setProductPrice( "$399" );

/**
* Get Object Properties
*/

$product = $product1->getProduct();
echo $product;

Output:

Output of above code will be,

Title: Smart Phone
Name: iPhone
Price: $399

Summary

  • I have taken three class properties in our above example.
  • I have written three methods to set the value for these properties.
  • I have written one class method to get the value of these properties.

Practice

  • You can write three more methods to get the value of each property individually like getProductTitle.(), getProductName and getProductPrice().

Note

  • I have included public visibility keyword in my method getProduct(), so it can be called from outside the class.
  • $this pseudo-variable: The $this pseudo-variable is the mechanism by which a class
    can refer to an object instance.

I hope you have enjoyed the tutorial about the basics of PHP5 class methods. Don’t forget to subscribe your email to stay in touch with tutorialchip.

3 thoughts

Comments are closed.