Difference between 'const' and 'define()'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason

    Difference between 'const' and 'define()'

    What is the difference between 'const' and 'define()' ? When would I
    prefer using 'const' over 'define', or vice versa? It seems if i do:

    const secondsInMinute = 60;
    define("seconds InMinute", 60);

    Aren't these two the same thing?

    - Jason

  • Kimmo Laine

    #2
    Re: Difference between 'const' and 'define()'

    "Jason" <jason.m.ho@gma il.comwrote in message
    news:1160632390 .133553.293300@ i3g2000cwc.goog legroups.com...
    What is the difference between 'const' and 'define()' ? When would I
    prefer using 'const' over 'define', or vice versa? It seems if i do:
    >
    const secondsInMinute = 60;
    define("seconds InMinute", 60);
    >
    Aren't these two the same thing?
    const defines a class constant, while define defines a global constant. You
    use const-defined constants inside class using self::constant, not
    $this->constant, and a define-defined constant in a normal, non-object shit.
    When using const, the value must be a static value, not a result of a
    mathematical operation or function. const is restricted in many ways
    compared to define, and should be used in classes/objects


    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • ZabMilenko

      #3
      Re: Difference between 'const' and 'define()'

      It's all about the scope.


      class BlahBlah
      {
      const MY_CONSTANT = 45676;
      }


      define('MY_CONS TANT', 76344);


      echo BlahBlah::MY_CO NSTANT . "\n" . MY_CONSTANT;


      // returns:
      // 45676
      // 76344


      Class constants are the closest thing to namespaces as php will get for this
      version.



      Jason wrote:
      What is the difference between 'const' and 'define()' ? When would I
      prefer using 'const' over 'define', or vice versa? It seems if i do:
      >
      const secondsInMinute = 60;
      define("seconds InMinute", 60);
      >
      Aren't these two the same thing?
      >
      - Jason
      >

      Comment

      Working...