views:

165

answers:

5

Hi,

I have a function prototype as such:

function do_upload( $file, $id, $type )

And I'm calling the function like this:

$this->do_upload( $files, $id, 'article' );

However, only $files is actually being passed through to the function. I'm sure it's simple but what have I done wrong?

EDIT:

So $file is just an array of file information, similar to $_FILES and it is passed through fine, I do some manipulation of it further in the function.

$id is set before I call the function, if I print_r() before the function call I see an ID I would expect and $type is just a string.

However, If I print_r() or die() on either $id or $type they are both blank and var_dump() returns the following:

die( var_dump( $id ) ); -> string(0) ""
die( var_dump( $type ); -> bool(false)

Right before the function call: die( var_dump( $id) ); -> string(3) "111"

Any ideas?

SOLUTION:

In case anyone has a similar problem, check the accepted answer below. Essentially I needed to pass the $files array by reference as it was using up the available stack space.

Thanks

A: 

How about checking for is_array()? I have seen much code in which they check for the variable, and if it's not an array they do something like:

if (!is_array($files)) }
   $f = $files;
   $files = new array();
   $files[] = $d
}

foreach( $files as $f ) {
 // whatever...
}
elcuco
+1  A: 

This actually looks correct. The first parameter is passed by reference which is not required as of PHP 5 (all objects and arrays are now passed by reference by default), but that shouldn't break anything. I would recommend removing the & anyway.

I think the question needs more context. Please post more of the surrounding code so people can help you figure out what's going on.

Asaph
Would the downvoter care to leave a comment?
Asaph
I've done this, and the array $file is still passing through okay but nothing else is :/
Adam Taylor
Please just post your code. If you show us your calling code and your function, you'll probably get your answer.
Asaph
+1  A: 

A few recommendations* concerning your code:

(1) use rather camel case notation than underscores for method names: doUpload

Reasons:

  • readability: distinguish native php functions easily
  • lazyness: write less, don't break your fingers for underscores
  • perception: Gestalt principles, keep related things closely together

(2) use public/private/protected function instead of just function, restrict as much as possible and open up later, if needed

Reason (from McConnell, Code Complete, 2nd Edition, p251): The difference between the "convenience" philosophy [a lot of global vars] and the "intellectual manageability" philosopy [as local as possible] boils down to a difference in emphasis between writing programs and reading them. Maximizing scope might indeed make programs easy to write, but a program in which any routine can use any variable at any time is harder to understand than a program that uses well-factored routines. In such a program, you can't understand only one routine; you have to understand all the other routines with which that routine shares global data. Such programs are hard to read, hard to debug, and hard to modify.

Consquently, you should declare each variable to be visible to the smallest segment of code that needs to see it.

(3) if you want to declare certain method parameters optional, don't set the default value with a wrong type. $id will most probably be an integer so don't set it to string with $id = ''. Set it for example 0 or null. (code readability, suggestion of wrong circumstances)

* a recommendation is an information of which the provider of the information believes it will help to improve the situation in question

tharkun
That´s not true, you can call a function with any variable name as it´s arguments, that´s completely unrelated to the name inside the function itself.
jeroen
sure, you're right jeroen, I wasn't thinking much it seems :)
tharkun
-1 for camel case and private. php is not java
stereofrog
I daresay you haven't read much best practices in php coding!
tharkun
Both Zend Framework as well as Symfony do this and recommend it!
tharkun
And who else would you think sets the standards of OO PHP?
tharkun
And why should OOP best practices be valid only for certain languages?
tharkun
i think the point here is that you're talking about *style* which isn't going to actually change anything here. also, nowhere was objects mentioned - it could be a global function. In any case, the tips might be good pointers, but won't help the OP.
nickf
@nickf, you're wrong. he's calling $this-> so it is a class method. and yes, my answer are general tips. This is a wiki after all!
tharkun
Camel-case notation vs underscores is just a matter of style. Zend and whoever else may recommend what they will, but it matters very little unless you happen to use their code. Event built in PHP objects, like mysqli, use underscores. Laying it out like you do; like camel-case *should* be used, without any explanation, is simply wrong.
Atli
He clearly states it as "recommendations" so ther is no "should" do this "should" do that.
Peter Lindqvist
+2  A: 

To answer the question "How to pass an array plus variables to a function in PHP?"

function f($x,$y){}
f(array(),$a);

Even if the structure is similar to this it does work as supposed to.

class a {
    function do_upload( &$file, $id, $type) {
    }
}
class b extends a {
    public function f() {
        $files = array('a','b');
        $id = "string";
        $this->do_upload($files,$id,'article');
    }
}

The only example i can give to reproduce the error in question is this. Perhaps someone can be more accurate?

class b extends a {
    function do_upload( &$file, $id, $type) {
        parent::do_upload($file, '', false);
    }
    public function f() {
        $files = array('a','b');
        $id = "string";
        $this->do_upload($files,$id,'article');
    }
}

I suggest you take another look at codeigniter

Peter Lindqvist
I'm not actually using the upload class of CodeIngiter, I've written my own and put it in a custom controller which all the classes needing upload functionality extend.
Adam Taylor
+1  A: 

I think your actually running out of stack space (or whatever space php uses internally for call stacks) for the function call. Try passing $files by reference. Equally try passing just a string to $files and see if that brings the other parameters back.

Paulo
This turned out to be absolutely the problem and passing $files by reference solved it.
Adam Taylor