top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

PHP: What is the name of the pattern that will ...

0 votes
238 views

I'm building a class which needs to have certain methods called by the subclass, but the subclass can extend but not obscure/override the behaviour.

So, for example, a method AuthRequestMade() will record the activity of making an authorisation request. It cannot make the actual request as that is the subclass's job, but, no matter what, I need to have this method called with the result of the actual auth request.

I want to make building the subclasses as simple and as fool proof as possible.

I think I have to do something like this ...

interface AuthEnforcer{
 public function AuthRequestMade(&$i_State, 
}

abstract class Auth implements AuthEnforcer{
 public method MakeAuthRequest(){
 // Do my stuff before.
 // Call the SpecificAuth class
 $this->AuthRequestMade($i_State, $s_Message);
 // Do my stuff after with state and message.
 }
}

class SpecificAuth extends Auth{
 public function AuthRequestMade(&$i_State, &$s_Message){
 // Do my specific stuff, setting state and message.
 }
}

But a couple of things I don't like (and don't know how to avoid).

1 - The SpecificAuth::AuthRequestMade is public and I want it protected as it shouldn't be called from the public scope.
2 - The response is by ref, but I think having a AuthResponse class containing $i_State and $s_Message should be enough there, but no way to enforce return types in PHP.

posted Jun 13, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

This is the Template Method pattern, though in this case you could use a Strategy where the specific authentication implementation is in a separate class that gets injected into the Auth class. As for your example there a a few things I would change.

  • The template method that the subclass must implement should not be declared by an interface. Interfaces are for declaring public contracts. You can simply declare an abstract, protected method in Auth. This is the contract that every subclass must fulfill.

  • I would avoid reference variables as you've indicated. If you don't want to build a data-holder class yet, simply return an array for now. While you cannot enforce the return type at parse time, they should be verified with unit tests. Unit tests are critical with dynamic languages like PHP and Python since runtime is the only way to verify behavior.

Otherwise, your example is spot on, though the name AuthRequestMade implies the request has already been made yet I think from your description that this method should *make* the actual request. Here's how I would write it with the above in place.

 class Auth {
 public function MakeAuthRequest() {
 // before
 $this->MakeAuthRequestImpl(); // Adding "Impl" suffix is a
common convention
 // after
 }

 /**
 * Make the actual authentication request.
 *
 * @return array Must contain keys "state" and "message" to hold
the result
 */
 protected abstract function MakeAuthRequestImpl();
 }
answer Jun 13, 2013 by anonymous
Similar Questions
+8 votes

If i want to create another MySQL database using PHP, then how can i create it with a different Name ?

+1 vote

I have changed my Php version from 7 to 5.
I got this error ==>

mcrypt_encrypt(): Key of size 10 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported

How to fix this?

+2 votes

Can someone give me an understanding of how the .ini settings are located and combined? I am under the impression that there is a full settings .ini file somewhere up high in my host's server tree and that
any settings I create in .ini files in each of my domain folders are appended/updated against the 'main' ini settings to give me a 'current' group of php.ini settings.

What I'm looking to find out is does an ini setting established in a test subdomain of my site affect those ini settings outside of my test subdomain?

...