I would say both syntax do exactly the same thing...
Edit : after writting the rest of the answer, actually, this is not entirely true ^^ It depends on what you declare ; see the two examples :
If you define Foo
as constructor, and call it with __construct
, it seems it's working ; the following code :
class Foo {
public function Foo() {
var_dump('blah');
}
}
class Bar extends Foo {
public function Bar() {
parent::__construct();
}
}
$a = new Bar();
Çıkışlar
string 'blah' (length=4)
Yani, tüm Tamam şimdi ;-)
On the other way, if you define __construct, and call Foo, like this :
class Foo {
public function __construct() {
var_dump('blah');
}
}
class Bar extends Foo {
public function Bar() {
parent::Foo();
}
}
$a = new Bar();
Size bir Ölümcül hata alırsınız:
Fatal error: Call to undefined method Foo::foo()
Sınıf eski sözdizimi ile bildirilirse Yani, bunu iki şekilde arayabilir; mantıklı, afterall :-) - yeni (PHP5) sözdizimi ile tanımlanmış eğer ve, bu yeni sözdizimi kullanmanız gerekir
BTW, if you want some kind of "real proof", you can try using the Vulcan Logic Disassembler, that will give you the opcodes corresponding to a PHP script.
EDIT after the comment
I've uploaded the outputs of using VLD with both syntaxes :
- vld-construct-new.txt : when declaring __construct, and calling __construct.
- vld-construct-old.txt : when declaring Foo, and calling __construct.
Iki dosya arasındaki diff yapıyor, bu ne olsun:
$ diff vld-construct-old.txt vld-construct-new.txt
25c25
< Function foo:
---
> Function __construct:
29c29
< function name: Foo
---
> function name: __construct
44c44
< End of function foo.
---
> End of function __construct.
71c71
< Function foo:
---
> Function __construct:
75c75
< function name: Foo
---
> function name: __construct
90c90
< End of function foo.
---
> End of function __construct.
(Unified diff is much longer, so I'll stick to using the default format of "diff" here)
Yani, demonte opcodes tek fark fonksiyonların isimleri; Foo
sınıfı ve Bar
sınıfı hem de (bu sınıfın __construct
/ Foo
yöntemi {[(0)] miras }).
Ne ben gerçekten söyleyebilirim olduğunu:
- Eğer PHP 5 kod yazma yapıyorsanız (and, in 2009, I sincerely hope you do ^^ ), daha sonra, sadece __ inşa sözdizimini kullanın
- Eğer (you should), daha sonra, Foo sözdizimini kullanmak PHP 5'e geçiş edemez bazı eski PHP 4 kodu korumak zorunda ...
As the sidenote, the documentation says (quoting) :
For backwards compatibility, if PHP 5
cannot find a __construct()
function
for a given class, it will search for
the old-style constructor function, by
the name of the class.
Effectively, it means that the only
case that would have compatibility
issues is if the class had a method
named __construct()
which was used
for different semantics.
Yani, gerçekten bir fark bu kadar değil düşünüyorum :-)
Did you encounter some kind of strange problem, that you think is caused by something like a difference between the two syntaxes ?