Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127846
Share on other sites

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?!

 

 

 

Are Namespaces used much?

 

Do they provide much value?

 

 

TomTees

 

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127878
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127883
Share on other sites

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;

 

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127894
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127902
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127907
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127927
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217168-constructors/#findComment-1127931
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.