简体   繁体   中英

OOP Diamond problem for php files - How should I deal?

I have file A.php that includes file B.php and C.php. Now both B.php and C.php includes D.php. D.php has some div tags, javascript functions/variables which all conflict. :(

I could copy paste code from D.php to B.php and C.php and rename functions/div tags. Is there a better solution that I could use ?

Use Include_Once instead of Include. This way the file will be included only once.

This is a quick fix, you should consider re-factoring your code if you are coming across this kind of problems.

Take the code that is in D.php and move it to another PHP file. Try to not get code and presentation mixed up as (as you can see) you run into a lot of problems. It may even be worth looking at the MVC pattern.

Why not move the functions somewhere else and include this in A, then leave the HTML in D and include it as before ?

As a general best practice, try to avoid mixing function declaration and presentation code (html, css... maybe even javascript)

Try to take a look at how a MVC framework is structured, this might help you get a better grasp on what I'm saying.

Don't mix HTML with any application logic. It just leads to problems such as yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM