You are here: Re: Current class name for use with :: « PHP Programming Language « IT news, forums, messages
Re: Current class name for use with ::

Posted by Jerry Stuckle on 05/03/06 20:51

Ciaran wrote:
> Andy,
>
> I think that 'self' will not do what you want, in this instance.
>
> In PHP, 'self' always refers to the class in which it is used so in
> your example code you will get a warning that A::TEST_CONST is not
> defined when you try either $b->printConst() or $c->printConst().
>
> This is very annoying behaviour, but there isn't a keyword in PHP5 that
> refers to 'the current class' in the way you want it to. This is under
> discussion for PHP6, and a new keyword apparently may or may not be
> introduced - I'm hoping very much that it will!
>
> See:
> http://www.php.net/~derick/meeting-notes.html#late-static-binding-using-this-without-or-perhaps-with-a-different-name
>

Actually, Ciaran, in this case it makes sense. For instance, what if you had:

$a = new A();
$a->printConst();

What would it print?


Or, if you had:

class A {
public function printConst()
{
print {something}::TEST_CONST;
}
}

class B extends A {
const TEST_CONST = 10;
}

class C extends B {
const TEST_CONST = 20;
}

$c = new C();
$c->printConst();

What would it print?

PHP does have an advantage over other OO languages such as Java and C++ in that
it is a strictly interpreted language - it's not compiled. So something like
this *could* be done.

However, the way to handle this would be to define TEST_CONST in A, and set it
in the constructors for B and C. Unfortunately, PHP doesn't allow a constant to
be initialized in the constructor, so it has to be a non-constant. However,
something like this works:

class A {
private $TEST_CONST = 30;

public function A($tc) {
$this->TEST_CONST = $tc;
}
public function printConst()
{
print $this->TEST_CONST;
}
}

class B extends A {
public function B () {
parent::A(10);
}
}

class C extends B {
public function C() {
parent::A(20);
}
}




--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

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

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