Reply to Re: Why is it possible to assign data to _not_declared_ vars in a class (PHP 5.0.3)?

Your name:

Reply:


Posted by Matthew Weier O'Phinney on 04/08/05 21:22

* Johannes Findeisen <mailman@hanez.org>:
> Hello All,
>
> Why is this working?
>
> <?php
>
> class foobar {
>
> public
> $a,
> $b;
>
> public function __construct() {
> $this->a = "Hello ";
> $this->b = "world! ";
> $this->c = "Good bye... ";
> }
>
> public function foo() {
> echo $this->a."<br>";
> echo $this->b."<br>";
> echo $this->c."<br>";
> }
> }
>
> ?>
>
> CALL:
> $test = new foobar();
> $test->foo();
>
> OUTPUT:
> Hello
> world
> Good bye...
>
>
>
> If i understand right, all variables should be declared in PHP5. So
> why is it possible to add a membervariable called "c" to the object
> without making a declaration? I got no error with this. I thought
> E_STRICT should show me things like that. Could someone explain me
> that?

You don't understand correctly. Class properties/attributes do not need
to be explicitly declared in PHP. This did *not* change in PHP5. What
changed in PHP5 is visibility. By default, unless declared otherwise, a
class attribute is publicly visible -- the same behaviour seen in PHP4.

--
Matthew Weier O'Phinney | WEBSITES:
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:matthew@garden.org | http://vermontbotanical.org

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация