Public Methods: Concept of PHP5 Classes

This tutorial is really delayed in PHP5 OOP Series due to important updates of TutorialChip freebies and premium products like Chip Life WordPress theme, Chip Prism Under Construction Template and others. However i have managed the product updates and feedback with the other members of TutorialChip, so there will be no delay in the quality tutorials of PHP and other resources in future.

This tutorial is about public methods / functions of PHP5 classes. I have already written about public properties in my previous tutorial, so i will recommend for the study of previous tutorial to understand the concept of public properties.

Public Methods: PHP5 Classes

Recommended Tutorials

I have used a lot of public properties and methods in my previous tutorials implicitly. And this is the time to solidify the concept of public methods in detail.

Elements in the classes can be declared public, private, or protected.

Public Methods

Public properties and methods can be accessed from any context.

Let’s have a look at the following examples to understand the concept of PHP5 Public Methods.

Example 1 – Public Methods

This is the very simple example to understand the concept of Public Methods. We will simply, access public property (productPrice) with the help of a public method.

/**
* PHP5 Public Methods / Properties
*/

class Product {

	/**
	* Class Properties
	*/

	public $productPrice = 499;

	/**
	* Class Methods
	*/

	/**
	* Get Product Price
	*/

	public function getProductPrice() {
		return $this->productPrice;
	}

}

/**
* Class Object
*/

$product = new Product();
echo "Price: $" . $product->getProductPrice();

Output of Example 1 will be,

Price: $499

Yes, we can access public properties of a class directly as,

$product->productPrice;

Let’s have another example to see the usefulness of accessing properties via methods,

Example 2 – Public Methods (Have Some Manipulation)

This is the second example to understand the concept of Public Methods. We will access public property (productPrice) with the help of a public method, but with some manipulation like discount, validation etc

/**
* PHP5 Public Methods / Properties
*/

class Product {

	/**
	* Class Properties
	*/

	public $productPrice = 499;

	/**
	* Class Methods
	*/

	/**
	* Get Product Price
	*/

	public function getProductPrice() {
		return $this->productPrice;
	}

	/**
	* Get Product Price
	* Discount Manipulation
	*/

	public function getProductPrice2() {
		$discount = 100;
		return $this->productPrice - $discount;
	}

}

/**
* Class Object
*/

$product = new Product();
echo "Price: $" . $product->getProductPrice();
echo "<br />";
echo "Price: $" . $product->getProductPrice2();

Output of Example 2 will be,

Price: $499
Price: $399

Points to Remember

  • Public properties and methods can be accessed from any context.
  • Be expert to access properties via methods instead of direct one.

I hope you have enjoyed the concept of public methods. Don’t Forget to Follow TutorialChip on Twitter or Subscribe to TutorialChip to Get the Latest Updates on Giveaways, Tutorials and More for Free.