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::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?
dimo414 [ Fr, 13 Juli 2007 12:14 ] [ ID #1767905 ]

Re: Static Attributes

At Fri, 13 Jul 2007 10:14:13 +0000, MichaelD let h(is|er) monkeys type:

> 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?

Try using testing::$connection instead of testing::connection.
HTH

--
Schraalhans Keukenmeester - schraalhans [at] the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"
Schraalhans Keukenmee[9] [ Fr, 13 Juli 2007 12:51 ] [ ID #1767906 ]

Re: Static Attributes

MichaelD wrote:

> 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.';
> }
> ...
> }

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

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

public function __construct($query)
{
if(!self::$connection)
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
http://tobyinkster.co.uk/blog/2007/06/28/demiblog-0.2.0/
Toby A Inkster [ Fr, 13 Juli 2007 14:09 ] [ ID #1767907 ]

Re: Static Attributes

On Jul 13, 12:14 pm, MichaelD <dimo... [at] 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.
darko [ Fr, 13 Juli 2007 15:44 ] [ ID #1767908 ]

Re: Static Attributes

At Fri, 13 Jul 2007 10:14:13 +0000, MichaelD let h(is|er) monkeys type:

> 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?

Try using testing::$connection instead of testing::connection.
HTH

--
Schraalhans Keukenmeester - schraalhans [at] the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"
Schraalhans Keukenmee[9] [ Fr, 13 Juli 2007 12:51 ] [ ID #1767963 ]

Re: Static Attributes

MichaelD wrote:

> 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.';
> }
> ...
> }

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

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

public function __construct($query)
{
if(!self::$connection)
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
http://tobyinkster.co.uk/blog/2007/06/28/demiblog-0.2.0/
Toby A Inkster [ Fr, 13 Juli 2007 14:09 ] [ ID #1767972 ]

Re: Static Attributes

On Jul 13, 12:14 pm, MichaelD <dimo... [at] 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.
darko [ Fr, 13 Juli 2007 15:44 ] [ ID #1767975 ]
PHP » alt.php » Static Attributes

Vorheriges Thema: Question of PHP and HTML Website
Nächstes Thema: RTF export: UTF-8 to ANSI conversion?