You are here: Re: Using a Resource as a Class Property « PHP Programming Language « IT news, forums, messages
Re: Using a Resource as a Class Property

Posted by Michael Fesser on 07/22/07 15:57

..oO(Sanders Kaufman)

>OK - that was weird, and took some debugging.
>I knew there was *something* about scope to deal with here.
>
>I'm aggragating the database object into a base class, and then creating
>other classes that "extend" that base class.
>
>HOWEVER, in order to do that, I had to move the instantiation(?) of the
>database class into the extended class.

This shouldn't be necessary, as long as you're correctly calling the
inherited methods from your derived class when necessary. Additionally
in PHP 4 there are no visibility options as in PHP 5 (public, protected,
private), so all member variables and methods are always public and
accessible from everywhere.

>When I had it in the base
>class, the class that extended it could not see the database properties
>and functions.

Hmm. Let's take your first example again (shortened):

class my_baseclass {
var $Database;

function my_baseclass() {
$this->Database = new my_database();
...
}
}

Now let's extend it:

class my_childclass extends my_baseclass {
}

This class will pretty much do the same as the base class, since no
methods are overwritten. But if you want to overwrite some methods to
extend their functionality, you should always also call the inherited
method. Let's assume you overwrite the constructor:

class my_childclass extends my_baseclass {
function my_childclass() {
// first call parent constructor or we won't have a database ...
parent::my_baseclass();
// then do some other stuff
...
}
}

Micha

 

Navigation:

[Reply to this 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

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