Archive for the 'OOP and Design Pattern Principles' Category

Private Methods in PHP

key

Key to Private Methods

Private Methods

The other day I was adding a private method to a PHP class, and I kept getting an unusual error. After multiple debugging attempts, I did a search on the Web and found the problem. (My problem for forgetting that PHP is different.) Like class properties the keyword $this needs to be involved.

Most readers of this blog probably know this already, but I’m including this little post to remind myself as much as anyone. Essentially, when you enter a private method in a class, it must be addressed as:

$this->pvtMethod();

In an of itself, that’s no great shakes, but if you don’t know that because you’ve immigrated to PHP from some other language such as C++, Java, ActionScript 3.0 or C#, you’re going to have a big problem with design patterns. That’s because in some design patterns you’re going to want to have private methods in some of the design’s participant classes.

Why Private Methods?

Why bother with a private method? Why bother with any private accessor or visibility? That’s easy:

Private visibility encapsulates your property or method.

As objects, we want everything in our classes to be treated like an integral part. If we want to give other classes access, we use getters and setters with public accessors. For example, consider the following program example:

<?php
ini_set("display_errors","2");
ERROR_REPORTING(E_ALL);
class PrivateProperties
{
	private $privateProp;
 
	function __construct()
	{
 
		print "From the constructor function <br />";
	}
 
	public function displayMethod()
	{
		print "This is from a public method.<br />";
 
		$this->privateProp=$this->privateMethod();
		print $this->privateProp;
	}
 
	private function privateMethod()
	{
		return "(Private Method): This only can be called from the class itself <br />";
	}	
}
$showtext=new PrivateProperties();
$showtext->displayMethod();

results:
From the constructor function
This is from a public method.
(Private Method): This only can be called from the class itself

The private method cannot be accessed directly from a call to it, but it can be launched from a public method within the same class. The function, displayMethod(), is public, but as part of the class, it can call the private methods in the same class. That’s exactly what it does.

While working with the private method, we don’t want to neglect a property with private visibility. In the same way that the $this statement is used with private methods, we also use it with private properties. As noted, this isn’t rocket science, but PHP is different from other languages, and this little reminder might come in handy when dealing with private visibility.

Share

Design Pattern Principles for PHP: Program to an Interface; not an implementation

Principal's Principle

Principles in Design Patterns and OOP

One of the things we’ve learned from our discussions of design patterns and OOP is that very few want to discuss principles. Put in a piece of code and there’ll be plenty of comments on how to tweak the code, but when we write about principles, we only get a few comments—if we get any at all.

We wish the opposite were true, and readers would gather up some code to illustrate their conception of a principle at work. We’re hoping that because PHP is a dynamic language (aka: weakly typed) there’ll be plenty of discussion of principles that were apparently created for strongly typed languages like Java, C# and ActionScript 3.0. In order to signal that a post is about a principle we’ve included an image of our favorite principal, Principal Seymore Skinner of the Simpsons. When you see Skinner, think OOP/Design Pattern Principle and I’ve got something to say about that!

The first principle of design patterns is,

Program to an interface, not an implementation

Simply put, the Gang of Four urges programmers to declare variables only to abstract classes and interfaces and not concrete implementations. You never want to type your instance as a concrete class derived from an interface or abstract class—only to the interface.

For any red-blooded PHP programmer, this may sound heretical because one does not declare types with either variables or functions. (Type hinting may do something like that, but more on it later.) However, after only a little reflection, we realize that PHP can indeed program to the interface by declaring variables (properties) and functions (methods) through classes that implement interfaces or extend abstract classes.
Continue reading ‘Design Pattern Principles for PHP: Program to an Interface; not an implementation’

Share