Static Attributes

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

    Static Attributes

    Just read a book on OOPHP and started playing around with PHP's OOP.
    Most of it's working just fine, but I cant seem to figure out how to
    access static attributes - here's my code:

    class testing implements Iterator
    {
    protected static $connection = false;
    ...

    public static function setConnection($ connection)
    {
    if(!testing::co nnection)
    $this->connection = $connection;
    }

    public function __construct($qu ery)
    {
    if(!testing::co nnection)
    echo 'Connection not set.';
    }
    ...
    }

    So here's my problem(s): first off, I get a Fatal error: Undefined
    class constant 'connection' at the line 'if(!testing::c onnection)' in
    the constructor. Using $this->connection doesn't work (and it
    wouldn't really make sense if it did) using self::connectio n causes
    the same error as testing::connec tion (which also makes sense) but I
    cant figure out how to refrence the attribute.

    That said, see the static function above? That gets by another class
    called before the constructor, and it works fine. Previously I used
    testing::connec tion = $connection; but that threw an error and told me
    to use $this-which doesn't make any sense at all, since there isn't
    a $this object to refrence.

    Can someone please explain both what I'm doing wrong and how to
    properly work with static attributes?

  • Toby A Inkster

    #2
    Re: Static Attributes

    MichaelD wrote:
    class testing implements Iterator
    {
    protected static $connection = false;
    ...
    >
    public static function setConnection($ connection)
    {
    if(!testing::co nnection)
    $this->connection = $connection;
    }
    >
    public function __construct($qu ery)
    {
    if(!testing::co nnection)
    echo 'Connection not set.';
    }
    ...
    }
    class testing implements Iterator
    {
    protected static $connection = false;
    ...

    public static function setConnection($ connection)
    {
    if(!self::$conn ection)
    self::$connecti on = $connection;
    }

    public function __construct($qu ery)
    {
    if(!self::$conn ection)
    echo 'Connection not set.';
    }
    ...
    }

    --
    Toby A Inkster BSc (Hons) ARCS
    [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
    [OS: Linux 2.6.12-12mdksmp, up 22 days, 15:48.]

    demiblog 0.2.0 Released

    Comment

    • Darko

      #3
      Re: Static Attributes

      On Jul 13, 12:14 pm, MichaelD <dimo...@gmail. comwrote:
      Just read a book on OOPHP and started playing around with PHP's OOP.
      Most of it's working just fine, but I cant seem to figure out how to
      access static attributes - here's my code:
      >
      class testing implements Iterator
      {
      protected static $connection = false;
      ...
      >
      public static function setConnection($ connection)
      {
      if(!testing::co nnection)
      $this->connection = $connection;
      }
      >
      public function __construct($qu ery)
      {
      if(!testing::co nnection)
      echo 'Connection not set.';
      }
      ...
      >
      }
      >
      So here's my problem(s): first off, I get a Fatal error: Undefined
      class constant 'connection' at the line 'if(!testing::c onnection)' in
      the constructor. Using $this->connection doesn't work (and it
      wouldn't really make sense if it did) using self::connectio n causes
      the same error as testing::connec tion (which also makes sense) but I
      cant figure out how to refrence the attribute.
      >
      That said, see the static function above? That gets by another class
      called before the constructor, and it works fine. Previously I used
      testing::connec tion = $connection; but that threw an error and told me
      to use $this-which doesn't make any sense at all, since there isn't
      a $this object to refrence.
      >
      Can someone please explain both what I'm doing wrong and how to
      properly work with static attributes?
      If you use the attribute without the string sign ($) you get constants
      declared in the class. Same as with ordinary variables.

      Comment

      Working...