2

In PHP I need to pass some arguments to a function by reference. I don't want to write 2 different methods for similar behaviour. So i need to select behaviour by argument. But I can't pass null by reference. So I created a dummy array.

So i run it either by

    $temp[0]=-1;
    $this->doSomething($bigIds, $temp);
or
    $temp[0]=-1;
    $this->doSomething($temp, $smallIds);


public function doSomething(&$bigIds, &$smallIds) {
        if ($bigIds[0] != -1) {
             // make some thing
        }
        if ($smallIds[0] != -1) {
             // make some thing
        }
}

Is there a better/ elegant way to do this?

hakre
  • 193,403
  • 52
  • 435
  • 836
trante
  • 33,518
  • 47
  • 192
  • 272
  • 2
    some context or real code would help. You could simply write one function with no references and return them so if you want to update vars - you can. ala `list($big, $small) = doSomething($big, $small)` – AD7six May 03 '12 at 20:06

2 Answers2

2

There could be loads of things you might rather do, for instance what @ad7six says in a comment, and you could also just give it some sort of setting and just one array..

public function doSomething(&$bIds, $mode) {
   switch($mode){
      case 1: do smallthing;break;
      case 2: do bigthing;break;
      case 3: do both;break;
      default: do nothing;break;
}

It all depends on what you need really

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • See my comment on sharkdrink's answer. Same applies to you. – Madara's Ghost May 03 '12 at 20:23
  • I'm not sure what you want, no polymorphism needed at all.... THe only issue I see is when you want to pass 2 parameters instead of just one (a usecase you didn't provide). Then just use the list() example in the commetns on your question. – Nanne May 03 '12 at 20:28
  • [Please watch these 38 minutes of pure education](http://www.youtube.com/watch?v=4F72VULWFvc). I hope it would open your eyes like it opened mine. When your function changes based on state, it's time to inherit. – Madara's Ghost May 03 '12 at 20:32
  • 2
    No need to, don't go preaching please. PHP does not support polymorphism in any sane way, and certainly with this little spec it would be rediculous to build a complete frame around this the fake it. Like I started, there are loads of ways to fix this properly, but in the end you would just need a good data model, no weird stuff like parameters being in random orders and a lot of more about the 'world' of this problem. So I'm not sure why you are preaching here, but my eyes don't need opening, I'm just not shooting a canon at a fly here. thank you. – Nanne May 03 '12 at 20:40
2

I would suggest an enum but this is PHP. So this should do it for you:

class YourClass
{
    const DoSomethingSmall = 0;
    const DoSomethingBig = 1;

    public function doSomething(&$ids, $actionType) {
        // can also use a switch here
        if($actionType == self::DoSomethingSmall) {
            // do small
        }
        else if($actionType == self::DoSomethingBig) {
            // do big
        }
    }
}

Then you can do:

$this->doSomething($bigIds, self::DoSomethingBig);
$this->doSomething($smallIds, self::DoSomethingSmall);

From outside the class you can use YourClass::DoSomethingBig and YourClass::DoSomethingSmall

Mark
  • 639
  • 8
  • 11
  • Your cases are substantially different. Your case requires polymorphism, while his generally requires method overloading (which isn't supported by PHP). The point is the order of arguments may change. – Madara's Ghost May 03 '12 at 20:22
  • How I have interpreted his question and the bit of code we have been provided is that he wants one function that operates on data in a similar manner with exceptions. These exceptions appear to be known before calling the function and the change in behavior can be indicated by a flag instead of his dummy array method. – Mark May 03 '12 at 20:35
  • Please see my latest comment on Nanne's answer. The case you're describing is tailored for polymorphism. – Madara's Ghost May 03 '12 at 20:37
  • Unfortunately, that was not what he asked. He said he didn't want to write two methods for similar behavior. Nanne and I have both provided answers to the question. If you want to suggest that the asker better use OO methods, then make that suggestion. We are just answering the question based on what he stated he needed. – Mark May 03 '12 at 20:41
  • 1
    You're the one who wrote a class here, not me :). In your case, I'm stating what's better. Also, answering the question isn't always solving the problem. – Madara's Ghost May 03 '12 at 20:43
  • Read his question, he's working with a class already. If you have a better answer, please provide it. – Mark May 03 '12 at 20:46
  • @Truth: I definitely agree with your statement `answering the question isn't always solving the problem`, but you can't solve the problem like this in PHP. So, what's the point? – Andreas May 03 '12 at 21:00