3

Consider this hypothetical scenario:

I have a Stomach class, which has a contents property, to which objects of class Food should be assigned. One way to do that would be to use a kind of a setter, e.g. setContents($food).

But lets suppose that we assign food directly, as in $stomach->contents = $orange.

Suppose also that whenever a Food object is assigned to contents we need to change the object's eaten property to true. Is there a way to do that without using a method of an object that it's being assigned to (in this case, the $stomach's setter)?

Basically, my question is: can we call a method whenever an object is assigned as a property to another object? Also, even if it is possible, is it bad design? Sorry if this is a stupid question, all of this is pretty new to me.

Stas Bichenko
  • 13,013
  • 8
  • 45
  • 83

3 Answers3

5

The best OOP solution here would be to create a method that indicates an action, like eat().

To ensure that the right Object is eaten, you could define an Interface (Say Food). This interface may define a method setEaten(). The Stomach (though I would prefer Mammal or something similar that actually can eat) can then call setEaten().

Since it created some controversy in the comments, I want to point out that an object definition should as closely as possible reflect what it actually does. To reduce object coupling it is best to avoid directly accessing object properties from an other class, however there is nothing wrong with using setters instead of actions when it makes sense (note that I used one in the definition of Food), and it may often depend on the developer view.

However, here it makes sense. Consider the case "A monkey eats a banana" (for simplicity please allow me to just materialize that banana out of thin air).

$m = new Monkey();
$m->eat(new Banana());

perfect. Now lets try a setter.

$m->setContents(new Banana());

Now we have a problem, the monkey already contains a lot of things, like bones, blood, muscles a brain, etc. So setContents makes no sense here. You could try $m->getStomach()->setContents(new Banana()) but that would just increase object coupling.

dualed
  • 10,262
  • 1
  • 26
  • 29
  • @pst This is mainly semantics. `setContents($object)` would only say that the stomach now contains `$object`, while `eat($object)` implies an action. For example you may want to call a callback when $object is eaten, but you may not when setting up your object. – dualed Dec 18 '12 at 20:54
  • 1
    And I *agree* about that entirely; my code favors *verb*-named actions. I just disagree about using "*correct* OOP" so seemingly off-hand. Neither is "more correct" although there are reasons to argue for `eat`; it doesn't change what is being done. –  Dec 18 '12 at 20:56
  • 2
    +1 This is *vastly* preferable to magic `__get` and `__set` which would be a wholly unnecessary obfuscation for such a straight-forward use-case. –  Dec 18 '12 at 20:59
  • @pst Readability and maintainability matter. If you re-read my comment, you'll notice it says nothing with regard to "correctness," only that this answer is "vastly preferable" to magic `__get` or `__set`. However, IMHO, this **is** the *de facto* correct way to "do it" in an OO paradigm because it's much, much better (readability and semantics-wise) than any other alternative. The fact that other alternatives exist doesn't mean this isn't the best option. You could also do it with convoluted static invocations -- and that would be a very poor choice. Dare I say even **incorrect.** –  Dec 18 '12 at 21:02
  • @pst I'm not sure why this is off-hand (there may be a language barrier here, on my side). OOP is not just using inheritance features of a language, it is using and defining objects sensibly. If something is an action, then the method name should reflect that. SetX is *usually* not an action. – dualed Dec 18 '12 at 21:07
  • @pst Please excuse me for sticking to PHP, which was the only language tagged for this post (not Java, not C#). For the OO paradigm in the PHP language, this is by far the "most correct" way to do it. The efficacy of the word "correct" in this context could be argued forever. Unfortunately, I assume the OP would like to eventually get some work done. As a result the usage of "correct" here is acceptable. That's all that matters. –  Dec 18 '12 at 21:08
  • 1
    @pst All that said, I agree with you :) I don't think "correct" is the best way to describe the solution. "Best where the PHP object model is concerned," would be preferable, but it's not enough to prevent me from upvoting. –  Dec 18 '12 at 21:11
  • @pst I hope you understand that OOP is a big topic before you dissect my edited answer and I would prefer not to rewrite one of the numerous books that already exist... – dualed Dec 18 '12 at 22:20
2

I agree with @dualed about the eat method. That said the way to acheive this to make all properties private/protected and then use __get/__set to proxy to the setters.

class Stomach {

   protected $contents;


   public function setContents(Food $food) {
      $this->contents = $food;
      $food->eaten = true;    
   }


   public function __set($name, $value) {
      $method = array($this, 'set' . $name);
      if(is_callable($method)) {
         return call_user_func_array($method, array($value));
      }
   }

  public function __get($name) {
     $method = array($this, 'get'.$name);
     if(is_callable($method)) {
        return call_user_func($method);
     }
  }
}

Youll notic i use is_callable as opposed to method_exists because if youre working on somehting complex with virtual methods is_callable should take those in to account whereas method_exists relies on the method being defined in the class hierarchy.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
1

__set function will help you here. But make sure you dont have any property named contents defined in class. Do it like this

class Stomach{
    private $props = array();
    public __set($prop, $value){
        if($prop === 'contents' and $value instanceof Food){
            $this->prop[$prop] = $value;
        }
    }
}
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Wouldn't this adversely affect the readability of code? This way it will be far harder to tell which properties the class has, won't it? – Stas Bichenko Dec 18 '12 at 20:43
  • 1
    @exizt for that I'd suggest to use @property doc comments. Doc comment will help you write code in IDEs. Also you can keep `private $allowed_props` to allow only some specific properties. – Shiplu Mokaddim Dec 18 '12 at 20:45
  • @exizt if you really want to define this property on the class dont use this method. Use `setContent` method instead. Thats more appropriate and OOP – Shiplu Mokaddim Dec 18 '12 at 20:46