Francisco Presencia
Francisco Presencia

Reputation: 8851

Defined variables and arrays vs functions in php

Introduction

I have some sort of values that I might want to access several times each page is loaded. I can take two different approaches for accessing them but I'm not sure which one is 'better'. Three already implemented examples are several options for the Language, URI and displaying text that I describe here:

Language

Right now it is configured in this way: lang() is a function that returns different values depending on the argument.

Example: lang("full") prints the current language, "English", while lang() prints the abbreviation of the current language, "en". There are many more options, like lang("select"), lang("selectact"), etc that print different things. The code is too long and irrelevant for the case so if anyone wants it just ask for it.

Url

The $Url array also returns different values depending on the request. The whole array is fully defined in the beginning of the page and used to get shorter but accurate links of the current page.

Example: echo $Url['full'] would print "http://mypage.org/path/to/file.php?page=1" and echo $Url['file'] would print "file.php". It's useful for action="" within the forms and many other things. There are more values for $Url['folder'], $Url['file'], etc. Same thing about the code, if wanted, just request it.

Text

[You can skip this section]

There's another array called $Text that is defined in the same way than $Url. The whole array is defined at the beginning, making a mysql call and defining all $Text[$i] for current page with a while loop. I'm not sure if this is more efficient than multiple calls for a single mysql cell.

Example: echo $Text['54'] prints "This is just a test array!" which this could perfectly be implemented with a function like text(54).

Question

With the 3 examples you can see that I use different methods to do almost the same function (no pun intended), but I'm not sure which one should become the standard one for my code. I could create a function called url() and other called text() to output what I want. I think that working with functions in those cases is better, but I'm not sure why. So I'd really appreciate your opinions and advice.

Should I mix arrays and functions in the way I described or should I just use funcions?

Please, base your answer in this:

Thank you

PS, now I know the differences between $Url and $Uri.

Upvotes: 2

Views: 159

Answers (2)

Jirka Kopřiva
Jirka Kopřiva

Reputation: 3089

I am using something like config array var. Where strings are set. Is better for later reading to use $LBL["hello"]='Hi!' than lbl(5). Think about yourself when you will return to your code aftre one year :)

Upvotes: 0

Mike B
Mike B

Reputation: 32145

It sounds like you're implementing ambiguous functions through the array notation. Normally, these would be classes with methods. $url['full'] would be $url->getFullPath(). Methods are preferred over the array accessor because methods are documented, and can be introspected by IDEs. Objects are more preferable because (in your examples) you can lazy-load the information. Right now, your script is compiling the $Url array and figuring out values for every possible key so it can be used in the script. Whereas a $request object could only do the parsing upon request - not instantiation.

Upvotes: 2

Related Questions