I have the following problem. I have an application that changes from year to year. Now I want to copy data between years. The way the application was design I have
/2009/Object1Class (attach to database db_2009)
/2010/Object1Class (attach to database db_2010)
Object1Class knows how to get data from the database for that specific year. Now what I want to do is
copy( from, to)
{
require "$from/Object1Class";
$obj_from = new Object1Class()
$data = $obj->get(); //got data from year
require "$to/Object1Class"; //ERROR CLASS ALREADY DEFINE
$obj_to = new Object1Class()
$obj->insert($data);
}
The problem, the require will throw an error since the class has already been define. Is there a way around this?
how include same class twice
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: how include same class twice
This is a horrible design. Why not have 1 class and use a variable to tell it what year it is working with?
If you're using 5.3 you can try namespaces.
If you're using 5.3 you can try namespaces.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: how include same class twice
I've never used them, but what if you declare a namespace around each inclusion of Object1Class? I believe this is just what namespaces were developed for.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: how include same class twice
I disagree. You see this is for taxes and each year is similar for previous one and they really dont share data with each other(except for copying data from one year to the next), So for each particular year , the concept of other years does not exist. It only exist the current year. So passing a variable really make no sense. In any case, is just one of those things that you will have to see in details. In my example I just put one class, in reality each years has hundreds of classes. The only way to describe it, is if you think about a whole ecosystem that exist for just one year. The idea of a different years, just does not exist once you are inside that system.AbraCadaver wrote:This is a horrible design. Why not have 1 class and use a variable to tell it what year it is working with?
I will take a look a namespaces. Thank you
Re: how include same class twice
If the classes do not represent the same entities, why give them the same names? Why not name them "Class2009" and "Class2010"?
I agree with the namespaces idea if renaming the classes is not an option.
I agree with the namespaces idea if renaming the classes is not an option.
Re: how include same class twice
My original idea was to make the program in such a way that I can just copy the previous folder code , with the new year, and just change the structures that change from the previous years. This idea actually turns out to be a good one, since I can change my code without worrying that is going to affect something(remember that you can file taxes 2008 taxes in 2010). So by copying the folder, I dont have to change any of my code that refer to an specific year, I just change the calculations that changed.