dsideal2015 2015-12-21 22:33
浏览 94
已采纳

在大规模建设时使用define(),Pros? 缺点?

I mass produce very similar sites, meaning they all use the same basic components, pages and are all single industry specific. These are my low end deeply discounted site designs. None of these sites ever get more than 20-30 visitors a day, so any extra load on the server isn't an issue.

In the interest of time, being that they all use the same components, though they may be in different locations or in a different order I would like to write one definition file that can be included with every site, so I can just call the defined constant instead of writing out the code a couple hundred times every year on every site I build. Also for editing later purposes this would make my life MUCH easier.

the definition file would look similar to the following:

define('UPCONTACT','<h1>Contact Us</h1>');
define('ULCONTACT','<a href="contact_us.php">Contact Us</a>');
define('UPABOUTUS','<h1>About Us</h1>');
define('ULABOUTUS','<a href="about_us.php">About Us</a>');

Obviously this is a very basic example but I think you get the idea.

So the question is what are the pros and cons of using define() in this manner?

  • 写回答

4条回答 默认 最新

  • doushang1778 2015-12-21 22:54
    关注

    It's pretty much ok. The disadvantage is that, given you are using constants, you can't override them for a single page or site.

    Use an array instead:

    config.php

     return array(
         'aboutus' => '<h1>About Us</h1>',
         'contactus' => '<a href="contact_us.php">Contact Us</a>'
     );
    

    include it like this in your site:

    $config = include('config.php');
    

    Then you can print it very easily

    <?php echo $config['aboutus'] ?>
    

    You can also change a value when you need it:

    $config = include('config.php');
    $config['aboutus'] = '<h1>About My Company</h1>';
    

    This is probably your best option.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn