1

I have a Class where most of the Methods are required to run. I normally call these methods after I've instantiated the Object.

<?php

try { 
   $obj = new MyClassName();
   $obj->Method1($var);
   $obj->Method2($var);
   $obj->Method3($var);
   $obj->Method4($var);

} catch(Exception $e) { 

}
?>

As my Class grows, and more Methods get introduced, I find that I need to ensure that certain Methods get called.

At first, I was calling all my required Methods within the __construct(), then calling those same Methods again, in case I need to alter things.

My other validation technique is to run get_object_vars() and check for the existence of specific properties.

I don't mind my current way to validate, but I have to inquire if there's an easier/better way.

coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
  • I would in generally say the constructor is the place for them –  Aug 12 '15 at 20:20
  • do you have to validate only certain methods or all ? – CodeGodie Aug 12 '15 at 20:21
  • 2
    This seems like a broken design to me, though it's so abstract it's hard to say. – Daniel Aug 12 '15 at 20:22
  • Not really knowing the code sounds kinda like you might benefit from some sort of [Dependency Injection](http://stackoverflow.com/questions/130794/what-is-dependency-injection). Have you read up much on design patterns? [PoEAA](http://martinfowler.com/eaaCatalog/) is a good read, still very relevant.. – ficuscr Aug 12 '15 at 20:22
  • 2
    What do you mean by required to run? Are they required to run before the object can be used for a certain thing? – Don't Panic Aug 12 '15 at 20:23
  • Sounds like you would benefit from creating interfaces for your structure since you are pretty much forcing that these methods are implemented in the class – CodeGodie Aug 12 '15 at 20:26
  • Those are all valid questions. I'm generalizing, for sure! Yes, certain Methods may become required. (and change later on) Too many ways to describe why. But, I only call Methods directly when I want to Customize that Method's usage. I'm pretty sure I'm doing all I can in the __construct() – coffeemonitor Aug 12 '15 at 20:32

1 Answers1

0

You can basically go in several ways. there's even a pattern called Chain of Responsability just for that!

Do you need to change the methods execution on the go? you can create some scenarios for example

class Scenarios 
{

   public $availableScenarios = array('carPayment' => 'car payment', 
                             'carStatus' => 'car status', 
                             'carPriceCalculator' => 'calculate car price');

   public function __consctruct($myChainedExecution)
   {

       foreach ( $this->$myChainedExecution() as $executedMethod ) 
       {

         if ( in_array(!$myChainedExecution ,array_values($this->availableScenarios))) {  
             Throw new \InvalidArgumentException('Process not recognized!');
         }             

         if( !$this->$executedMethod() ) {
             Throw new \BadMethodCallException("error on $executedMethod method ");
         }

         return true;            

       }     

   }

   public function carPayment()
   {
      return  array('method1',
                    'method2',
                    'method3');
   }


  //would return true or false using attributes for internal values
  public function method1(){}
  public function method2(){}
  public function method3(){}

}

So when you call $process = new Scenario('car payment'); I know that the code takes cares of that internally with a try, some execeptions you'll have everything covered

AleMelo
  • 128
  • 4