PHP5: const and class scope problems

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

    PHP5: const and class scope problems


    Say I have a class with a constant in it. I also have a variable in
    that class that I would like to set to that constant as the
    initialization value. Why doesn't the following work?

    class Square
    {
    const DEFAULT_SIZE=5;
    var $size=self::DEF AULT_SIZE; // how do i set $size to DEFAULT_SIZE?
    }

    $r= new Square();

    Errors are:

    PHP Fatal error: Cannot access self:: when no class scope is active in
    C:\src\test\con sttest.php on line 9

    Fatal error: Cannot access self:: when no class scope is active in
    C:\src\test\con sttest.php on line 9

    Which is wrong, because a class scope is active!! I think it is trying
    to inline the code, and it is doing it incorrectly.

    -d
  • Berislav Lopac

    #2
    Re: const and class scope problems

    Doug wrote:[color=blue]
    > Say I have a class with a constant in it. I also have a variable in
    > that class that I would like to set to that constant as the
    > initialization value. Why doesn't the following work?
    >
    > class Square
    > {
    > const DEFAULT_SIZE=5;
    > var $size=self::DEF AULT_SIZE; // how do i set $size to DEFAULT_SIZE?
    > }
    >
    > $r= new Square();[/color]

    Bad OOP. :)

    Try this:

    class Square {

    const DEFAULT_SIZE = 5;
    public $size; // 'var' is deprecated in PHP5

    public function __construct() {
    $this->size = $this->DEFAULT_SIZE ;
    }

    }

    $r = new Square();


    Comment

    • Doug

      #3
      Re: const and class scope problems



      Berislav Lopac wrote:
      [color=blue]
      > Bad OOP. :)[/color]

      On the contrary, In I believe this is great OOP. :) Even if it is bad
      OOP, it still shouldn't cause a compile-time error. Constant
      initializers are allowed for any variable.

      Example:

      class Square {
      public $size=5;
      }

      $r = new Square();

      works just fine. As does this example given in the PHP5 manual:

      class SimpleClass
      {
      // member declaration
      public $var = 'a default value';

      // method declaration
      public function displayVar() {
      echo $this->var;
      }
      }

      From the manual: In PHP 4, only constant initializers for var variables
      are allowed.

      And that goes for PHP 5 too. This even works:

      class Constants
      {
      const DEFAULT_SIZE=5;
      }

      class Square
      {
      var $size=Constants ::DEFAULT_SIZE;
      }

      The ONLY difference is that when you try to use a constant from the
      class' own definition. In that case, it can not find the constant,
      saying no class scope is active.

      -d



      [color=blue]
      > Try this:
      >
      > class Square {
      >
      > const DEFAULT_SIZE = 5;
      > public $size; // 'var' is deprecated in PHP5
      >
      > public function __construct() {
      > $this->size = $this->DEFAULT_SIZE ;
      > }
      >
      > }
      >
      > $r = new Square();
      >
      >[/color]

      Comment

      • Zurab Davitiani

        #4
        Re: const and class scope problems

        Doug wrote:
        [color=blue]
        > The ONLY difference is that when you try to use a constant from the
        > class' own definition. In that case, it can not find the constant,
        > saying no class scope is active.[/color]

        Maybe because the class is not completely loaded at that point.

        Comment

        • Samuel Cochran

          #5
          Re: const and class scope problems

          Zurab Davitiani wrote:[color=blue]
          > Maybe because the class is not completely loaded at that point.[/color]

          Indeed, because it is an interpreted language, PHP wouldn't create a
          scope for the class until AFTER the class has bee interpreted. Remember
          that rule about only being able to reference stuff AFTER it's declared?
          well, you can only reference the class AFTER it's been declared, which
          means the constant too, as it is part of the class.

          Comment

          Working...