TomTees Posted October 29, 2010 Share Posted October 29, 2010 I know this is a PHP site, but is it true that in Java the constructor has the same name as the class name? I went to the PHP.net manual and it has a similar set up. Is that correct? I thought there was some other convention that differed from Java? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/ Share on other sites More sharing options...
btherl Posted October 29, 2010 Share Posted October 29, 2010 That's the old convention in PHP. The new magic constructor name is __construct(). I can't help with Java, as you said, this is a PHP board Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127839 Share on other sites More sharing options...
mikosiko Posted October 29, 2010 Share Posted October 29, 2010 http://php.net/manual/en/language.oop5.decon.php Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127840 Share on other sites More sharing options...
TomTees Posted October 29, 2010 Author Share Posted October 29, 2010 As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes. Example #2 Constructors in namespaced classes <?php namespace Foo; class Bar { public function Bar() { // treated as constructor in PHP 5.3.0-5.3.2 // treated as regular method as of PHP 5.3.3 } } ?> I guess that means if I wanted to use the old-style to mimic Java it would backfire after v5.3.3, right? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127846 Share on other sites More sharing options...
trq Posted October 29, 2010 Share Posted October 29, 2010 I guess that means if I wanted to use the old-style to mimic Java it would backfire after v5.3.3, right? It only effects non-namespaced classes. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127857 Share on other sites More sharing options...
TomTees Posted October 29, 2010 Author Share Posted October 29, 2010 I guess that means if I wanted to use the old-style to mimic Java it would backfire after v5.3.3, right? It only effects non-namespaced classes. What does that mean in English?! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127860 Share on other sites More sharing options...
btherl Posted October 29, 2010 Share Posted October 29, 2010 It means "If you don't know what a namespaced class is, it doesn't affect you" Here's what the manual says: http://php.net/manual/en/language.namespaces.rationale.php Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127868 Share on other sites More sharing options...
TomTees Posted October 29, 2010 Author Share Posted October 29, 2010 It means "If you don't know what a namespaced class is, it doesn't affect you" Kind of like gravity and Wile-E-Coyote?! Here's what the manual says: http://php.net/manual/en/language.namespaces.rationale.php Are Namespaces used much? Do they provide much value? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127878 Share on other sites More sharing options...
btherl Posted October 29, 2010 Share Posted October 29, 2010 I've never used them myself. I can see how they could be useful in large applications where some libraries might have conflicting class names or similar issues. I imagine it would be useful to allow code from two monolithic applications to be integrated without a lot of renaming. Perhaps both apps have a "Db" class with a different interface. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127883 Share on other sites More sharing options...
trq Posted October 29, 2010 Share Posted October 29, 2010 Most people who write allot of OOP in PHP use a form of psuedo namespacing anyway, but now it can be done more convenietly with 5.3. Given the directory structure of part of a simple framework. [pre] └── Foo ├── IO │ ├── Http │ │ ├── Header.php │ │ ├── Request.php │ │ └── Response.php │ ├── Request │ │ └── RequestAbstract.php │ └── Response │ └── ResponseAbstract.php ├── Loader │ └── Exception.php └── Loader.php [/pre] The Dispatch class (within the Foo/Controller/Dispatch.php file) would generally be defined by.... class Foo_IO_Http_Request extends Foo_IO_Request_RequestAbstract {} and called by .... $r = new Foo_IO_Http_Request; With 5.3 and namespaces you can now do. namespace Foo/IO/Http; use Foo/IO; class Request extends Request/RequestAbstract {} namespace Foo/IO/Http; $r = new Request; Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127894 Share on other sites More sharing options...
Anti-Moronic Posted October 29, 2010 Share Posted October 29, 2010 If you've every used a framework, you'll see the importance in namespaces and common naming conventions - especially in large, complex applications. When you move into OOP this is one of those things you learn along the way, if not already. Thanks for that info thorpe! Very interesting. I've not looked into 5.3 much. I tend to worry too much about compatability int he beginning. Hopefully it will be widely updated in a short space of time. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127902 Share on other sites More sharing options...
sharal Posted October 29, 2010 Share Posted October 29, 2010 You should consider namespaces in vast projects containing a lot of code, and possibly where you are multiple employees working on the project. It will save you a lot of potential problems if you use pseudo namespaces as pointed out by Thorpe. As for the constructor, it's best sticking to the magic method __construct(), for instance, when you rename your class, you won't have to rename your constructor. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127907 Share on other sites More sharing options...
salathe Posted October 29, 2010 Share Posted October 29, 2010 It only effects non-namespaced classes. Just for the record, it only effects namespaced classes. Classes not under a namespace (i.e. in the "global namespace") can still use constructors named after their class. That said, class-name-based constructors went out of fashion years and years ago with __construct() being much more preferred. Finally, the namespace separator is the backslash character (\) and not the solidus (forward slash, /) as thorpe used. In other words, Foo\IO\Http. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127927 Share on other sites More sharing options...
trq Posted October 29, 2010 Share Posted October 29, 2010 It only effects non-namespaced classes. Just for the record, it only effects namespaced classes. Classes not under a namespace (i.e. in the "global namespace") can still use constructors named after their class. That said, class-name-based constructors went out of fashion years and years ago with __construct() being much more preferred. Finally, the namespace separator is the backslash character (\) and not the solidus (forward slash, /) as thorpe used. In other words, Foo\IO\Http. I'm on fire today. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127931 Share on other sites More sharing options...
ignace Posted October 29, 2010 Share Posted October 29, 2010 Namespaces also allows for monkey patching: namespace Foo\Bar; function strlen($str) { return 3; } All code run under this namespace will return 3 as a strlen() result. Quote Link to comment https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.