How about testing for the parent? Such as:
<?
class AbstractClassException { }
class MyClass
{
var $salutation;
function MyClass()
{
if(!get_parent_class($this))
{
throw new AbstractClassException();
}
$this->salutation = 'Hello';
}
function test()
{
print($this->salutation);
}
}
class MyOtherClass extends MyClass
{
function MyOtherClass()
{
MyClass::MyClass();
}
}
//use the abstract class correctly
$c = new MyOtherClass;
$c->test();
//use it incorrectly, get an exception
$c = new MyClass;
?>
Regards,
Leon
----- Original Message -----
From: "Jens Rehsack" <[EMAIL PROTECTED]>
To: "Andrei Zmievski" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Saturday, November 09, 2002 8:22 AM
Subject: Re: [PHP-DEV] abstract functions
> Andrei Zmievski wrote:
> > ZE1 way:
> >
> > class MyClass {
> > function MyClass()
> > {
> > die('MyClass is an abstract class');
> > }
> > }
> >
> > ZE2 way:
> >
> > class AbstractClassException {
> > }
> >
> > class MyClass {
> > function MyClass()
> > {
> > throw new AbstractClassException();
> > }
> > }
> >
> > On Sat, 09 Nov 2002, Jens Rehsack wrote:
>
> That's not ok, cause (very simple)
> class Calcer
> {
> function Calcer() { die('abstract'); }
> function ShowResult( $a )
> {
> echo $this->CalcResult( $a )
> }
> abstract function CalcResult( $a );
> }
>
> class Square extends Calcer
> {
> function Square()
> {
> Calcer::Calcer(); // cause inherited could do sth.
> }
>
> function CalcResult( $a )
> {
> return $a * $a;
> }
> }
>
> class Adder extends Calcer
> {
> var $Summand;
>
> function Adder( $aSummand )
> {
> Calcer::Calcer();
> $this->Summand = $aSummand;
> }
>
> function CalcResult( $a )
> {
> return $a + $this->Summand;
> }
> }
>
> I can give a more complex example, if you want, but it's not ok to die
> in constructor of an abstract class, cause the main logic could be
> implemented in this class and it needs to be derived cause for helper
> functions...
>
> An example is a cache control class which is able to access cached
> objects through it real name and is either able to store in filesystem
> or in database.
>
> Jens
> >>Hi,
> >>
> >>does PHP4 with the ZE2 supports abstract function like Delphi or C++?
> >>That would be very useful for class development, cause we can avoid
> >>testing if a class is abstract if an abstract class couldn't be
> >>instantiated?
> >>
> >>Syntax could be like in C++
> >>class X
> >>{
> >> X(){}
> >> int y() = 0;
> >>}
> >>
> >>or little bit more like pascal
> >>
> >>class X
> >>{
> >> X(){} // PHP
> >> function x(); abstract;
> >>}
> >>
> >>or
> >>
> >>class X
> >>{
> >> X(){} // PHP
> >> abstract function x();
> >>}
> >>
> >>Greetings,
> >>Jens
> >>--
> >>L i W W W i Jens Rehsack
> >>L W W W
> >>L i W W W W i nnn gggg LiWing IT-Services
> >>L i W W W W i n n g g
> >>LLLL i W W i n n g g Friesenstra�e 2
> >> gggg 06112 Halle
> >> g
> >> g g
> >>Tel.: +49 - 3 45 - 5 17 05 91 ggg e-Mail: <[EMAIL PROTECTED]>
> >>Fax: +49 - 3 45 - 5 17 05 92 http://www.liwing.de/
> >>
> >>
> >>--
> >>PHP Development Mailing List <http://www.php.net/>
> >>To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >
> >
> > -Andrei http://www.gravitonic.com/
> > * A feature is a bug with seniority. *
> >
> >
>
>
>
> --
> L i W W W i Jens Rehsack
> L W W W
> L i W W W W i nnn gggg LiWing IT-Services
> L i W W W W i n n g g
> LLLL i W W i n n g g Friesenstra�e 2
> gggg 06112 Halle
> g
> g g
> Tel.: +49 - 3 45 - 5 17 05 91 ggg e-Mail: <[EMAIL PROTECTED]>
> Fax: +49 - 3 45 - 5 17 05 92 http://www.liwing.de/
>
>
>
> --
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php