-2

I have a website & need to use list of countries in multiple pages. On some pages I need to display the list & on some pages I need to populate a dropdown from that list. For this I am aware of following methods. Which one is better & why or is there any other preferable option. I read somewhere that using static keyword slows down the code and creating Object everytime also slows down the code as it allocates and consumes memory every time. So please put some light on what's the best way out.

Method A :

class Country
{

      public static $CountryList = array('Country 1', 'Country 2', .....);

} 

    In my pages I will use it with : 

    $Countries = Country :: $CountryList;

Method B :

        class Country
        {

              public $CountryList = array('Country 1', 'Country 2', .....);

        } 

     In my pages I will use it with : 

     $Obj = new Country();

     $CountryList = $Obj->CountryList;

Method C :

   class Country
   {

        private $CountryList = array('Country 1', 'Country 2', .....);


        public static function getList()
        { 
           return $this->CountryList;
        } 


   }  

         In my pages I will use it with : 

         $Obj = new Country();

         $CountryList = $Obj->getList();
sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • Take a look at [constants-containing-arrays](https://stackoverflow.com/questions/1290318/php-constants-containing-arrays) – B001ᛦ May 24 '18 at 10:40
  • @B001ᛦ : I already know the const option, but I want to know that is static keyword or creating object not a correct way ? – sqlchild May 24 '18 at 10:42
  • Well you have tagged with `constants` – B001ᛦ May 24 '18 at 10:43
  • To be more _future-proof_, you could insert them in a database, it would allow you to create a page to add/remove/rename some countries. Otherwise if you really want to use this in a class, the Method C allows you to encapsulate the data. By encapsulating the data, you would be able to create a filter according to a parameter or something similar, it's easier to manage in the time. But if you're absolutely sure you won't update this variable you could use the `const` keyword instead of `static` (from PHP 7). – Anthony May 24 '18 at 10:46
  • @B001ᛦ : Yeah man! Can you let me know that is using const better than the static & obj memory allocation method ? – sqlchild May 24 '18 at 10:46
  • 2
    _...using const better than the static & obj memory allocation..._ Absolutely! constants were designed for such a requirement :) And you don't need to write/maintain any classes + instantiate objects etc.... so get rid of the whole overload ;) – B001ᛦ May 24 '18 at 10:49
  • @B001ᛦ : why did I get downvotes ? am I completely wrong asking this type of question or what's the wrong point here ? – sqlchild May 24 '18 at 10:54
  • @AnthonyB : And what about memory consumption in the class method ? Will it consume too much memory if I use it everywhere to display and fetch countries? – sqlchild May 24 '18 at 10:55
  • @AnthonyB : Also what do you mean by Are you sure you won't update this variable ??? I can add some more countries if I want ain't that possible ? – sqlchild May 24 '18 at 10:56
  • @B001ᛦ : People have written that the CONSTANT method should be deprecated and is too slow in performance. – sqlchild May 24 '18 at 10:59
  • 1
    @sqlchild I'm not one of the downvoters, but I think you got downvotes because it's not a pretty beautiful way to store datas. If one day you want to add a country, you would be obliged to update your source code, commit it, test it, push it onto production. To store datas, it's really better to use a database, even a little table can be good to be _future-proof_, it's easy to add a country and your code works the same. For your first comment, the `const` class variable is compiled so it takes very less memory. You can use a `const` class variable, and use a getCountries method to get them. – Anthony May 24 '18 at 11:00
  • @AnthonyB : So the getCountries method should be static or non-static (everytime would have to be called via an instance which would I guess consume more memory) – sqlchild May 24 '18 at 11:09
  • There is no 'absolute' rule about it, it really depends on your case. If you need to store datas on an object to customize the behaviour it may be non-static, but if you are sure you don't need it, it can be static. Could you explain more the usage of this list? According to its concrete usage, the answer is different. Also, why don't you want to use a database? – Anthony May 24 '18 at 11:13
  • @AnthonyB : Thanks Anthony for your time pushing great answers and helpful comments on SO. I don't want to use database as it would be more lengthy for me , storing, then retreiving and coding for all this. I just want to hard code it and use it – sqlchild May 24 '18 at 11:23
  • @AnthonyB : So what would you suggest : $Obj->getCountries() or $Countries = Country :: CountryList; – sqlchild May 24 '18 at 11:25
  • _People have written that the..._ Well.. people write a lot of stuff... you can test by yourself @sqlchild – B001ᛦ May 24 '18 at 11:46
  • As said by @B001ᛦ, you probably should test by yourself according to what is easier/better for you. `$Obj->getCountries();` is more flexible than the other solution, but it's up to you. – Anthony May 24 '18 at 11:54

1 Answers1

1

As I said in comments I really would prefer using a database. It is more flexible and future-proof. You could have a table country that contains an ID, an ISO code and whatever you want, an other table country_lang that contains the translations of the country names. Doing so, your code keeps being the same and working the same, there is no update to do.

But if you really want to keep the countries in a variable without a database, you can use many solutions according to your need.

1 - Plain old constant

If you are using PHP 7.0+, you can use a plain old constant.

<?php
define('COUNTRIES', ['a', 'b']);
var_dump(COUNTRIES);

Pros:

  • If defined in a front controller or any global file, the constant is available everywhere in the application.
  • Because it's a constant you are sure about its integrity during the execution, no method can update its value.

Cons:

  • It is not future-proof, if one day you want to add informations about it (like translations or ISO codes) it's pretty hard.
  • If you use this value in some functions/methods, these functions/methods are not flexible enough to be used in many contexts.

2 - Class constant

If you are using PHP 5.6+ you can use a class constant.

<?php
class Country
{
    const COUNTRIES = ['a', 'b'];
}

Pros:

  • It's encapsulated in a class so it's easier to add it in any method. You could add some methods that take it as parameter.

Cons:

  • It's not future-proof, if you want to add some informations like ISO codes or translations it's hard.

3 - Static attribute

You could use a static attribute.

<?php
class Country
{
    public static $countries = ['a', 'b'];
}

Pros:

  • It's encapsulated in the class.

Cons:

  • It's a public attribute so any function/method could update the value.
  • It's not future-proof, it's hard to add informations like ISO codes or translations.

Anyways it's up to you, according to your need in this specific case to choose what method is the best for you.

Anthony
  • 2,014
  • 2
  • 19
  • 29