开发者

How to pass an array plus variables to a function in PHP?

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开发者_C百科 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


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.


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


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.


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


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...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜