Object creation variable construct values

I am trying to figure out if there is a solution for the following problem:

Lets say I have a class named OpenFiles. The constructor requires a path to what file to open and an Object which is required for OpenFiles.

class OpenFiles
{
    public function __construct(Object $object,$filePath)
    {
        //code      
    }
}

The $object is always the same can could be found in some sort of container. But the filePath is different each time this class is needed.

My 'problem' is when this class changes I need to go over all my code and edit accordingly.

Is there a pattern or solution for this? or is this inevitable?

Thanks in advance!

Answers


I would suggest using an interface for your object and use the interface as the input param for your function.

interface iPermissionCollection
{
    public function getPermission($name);
}

then change the class to use the interface instead of the object.

class OpenFiles
{
    public function __construct(iPermissionCollection $object,$filePath)
    {
        //code      
    }
}

and if you want to send a different class later just be sure that it will implement iPermissionCollection.

So the question is not how would i handle code refactoring if i chage the class constructor that i use multiple time but the question is how would i write such a class that uses exactly what it needs and then i could scale.


Need Your Help

PHP loop through array returned by MySQL function

php mysql arrays function loops

The code below works fine. However I would like to output the results using a loop. I can do it by going through each key individually or as $post[0] for example but not using a loop to go through ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.