|
Posted by Darko on 07/13/07 13:44
On Jul 13, 12:14 pm, MichaelD <dimo...@gmail.com> wrote:
> 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::connection)
> $this->connection = $connection;
> }
>
> public function __construct($query)
> {
> if(!testing::connection)
> 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::connection)' in
> the constructor. Using $this->connection doesn't work (and it
> wouldn't really make sense if it did) using self::connection causes
> the same error as testing::connection (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::connection = $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.
Navigation:
[Reply to this message]
|