Re: Re: PHP Functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



While PHP lets you write anonymous functions like that, and they let you do some very cool things, it should not be your first go-to. Unless you're inlining a bit of functionality into an array_map() call or similar, or you're trying to capture some context with use(), you probably should just use a named function:

function vehiclesOutput($newVhcls=array(), $header=true) {
	...
}


I'm going to venture a guess that you're coming from Javascript, where named functions are legal but convention says to never use them. PHP is the other way around. :-) Most of your code will be normal named functions, or better yet defined classes. (Unless you're taking a purely-functional approach, which in PHP is a fairly advanced and still-fringe style that is still working its way into vogue.)

A few other notes:

- Don't use abbreviated variable names. That offers no value other than making the code harder to read. Ie, $newVehicles.

- Type as much code as you can. That is, if $newVehicles is an array, specify that in the function signature:

function vehiclesOutput(array $newVhcls=array(), $header=true) { ... }


You can type any class, interface, or "array". In PHP 7 (due out next month, we expect) you can also type on primitives (string, bool, int, float). You should use those, too.

- If you're new to PHP but not to programming (as seems to be your situation), take the time to read this site:

http://www.phptherightway.com/

Most PHP tutorials online that is older than 5 years is harmfully wrong. Start there for a well-curated set of leading practices for good modern PHP code.

Cheers.

--Larry Garfield

On 10/31/2015 03:43 AM, Karl DeSaulniers wrote:
Sorry for the spam people. Looks like I was echoing $vehicleOutput() instead of $vehiclesOutput() with an "s".

Still, if this is the incorrect way of doing this please let me know.
I want to plan my code so others can work with it with "some" ease.

TIA

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com



On Oct 31, 2015, at 3:08 AM, Karl DeSaulniers <karl@xxxxxxxxxxxxxxx> wrote:

Hello all,
Given the following function writing technique.

$vehiclesOutput = function($newVhcls=array(), $header=true) {
	...
};

Everything was ok till I put another function in below it in a similar fashion.
(Assigning the function to a variable like so)

Then I got this error.

Function name must be a string

but only on the second function and not all the time. It's weird.

My first question is obvious. Am I doing this wrong?
I got this way of writing functions from another coder online in my researching.
So this is not my creation per se. I know about declaring function before the name way as well, just liked this style better I guess.
If it is the correct way, what is happening?

Sorry if this is a newbie question, but I haven't fully molted yet.
If there is a doc I should be looking at, please provide gently if you could.

TIA

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux