Defined variables and arrays vs functions in php

Go To StackoverFlow.com

2

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:

  • The source needs to be readable and reusable by other developers
  • Resource consumption (processing, time and memory).
  • The shorter the code the better.
  • The more you explain the reasons the better.

Thank you

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

2012-04-04 23:07
by Francisco Presencia


2

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.

2012-04-04 23:20
by Mike B
I don't understand you completely. What do you mean by "methods"? Magic methods? Also, if you stated that I'm using objects, why do you use "whereas a $request object..." to contrast with my script? Aren't both sentences about objects? Sorry, my English is not good.. - Francisco Presencia 2012-04-04 23:36
I would definitely also turn both lang, url and text (although I'd probably merge that with lang) into classes. But I think in this case I'd use static classes and then use them like: URL::getFullPath() or Lang::get(54) (although I'd never use numeric keys for translations) - powerbuoy 2012-04-04 23:43
With your example every variation of the variable you pass in should be a concrete method. With the exception of dynamic values. For example: your $url array would be turned into a $request object. You would invoke its concrete methods like $request->getFullPath().. you could also do $request->getParam('page'). You don't need magic methods for what you're doing - They always make things more complicated but have their uses. $request was me referencing the object version of your $url array - Mike B 2012-04-04 23:46
Thanks about the classes, I'll get to learn about them. Also, I've given a lot of thought about the numeric keys for translations, and since there will be hundreds/thousands of arrays in several languages, I decided this was the best way. To input text, I have the source page and a translation page opened and switch between them, so there's no problem getting confused - Francisco Presencia 2012-04-04 23:49


0

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 :)

2012-04-04 23:27
by Jirka Kopřiva
I wouldn't really need to define many globals (not to say none), as the functions are only called to print something in html. Actually, the same code use would be: echo $Url["full"]; or url("full"). Edited in the main question - Francisco Presencia 2012-04-04 23:39
When you call a function, where local vars are set you are doing the same. You only define many locals. Same cost of memory - Jirka Kopřiva 2012-04-04 23:41
hint - look into code of some open source SW. Its made by the bests. Drupal, Wordpress etc - Jirka Kopřiva 2012-04-04 23:45
There's no problem about the numeric values, there's another script that easily allows me to know what number is what. Also it's easier to mistake a letter than a number and I avoid the pain of making up thousands of names for the different text arrays. Will do about peaking into open source software like Drupal. Thank - Francisco Presencia 2012-04-04 23:52
Ads