dqx13503925528 2016-04-08 17:04
浏览 39
已采纳

PHP文件结构:变量和函数

I have worked in PHP before, but mainly some small touches that actually concern the output HTML. I am more of a front-end guy. Now, I am building a small-scale website that has a lot of duplication in the head, header, and footer but differs a bit in content (almost your typical "template" style design). I am not sure how I should handle dependencies in such scenario. I have variables, I have functions, and I have includes and requires. But where goes what?

For instance. I have a config.php file that contains a couple of global static variables that are often used (such as $home which is the base url - this is necessary for testing locally in subdirectories). I require this config file at the top of each other PHP file. I think that should be sort of alright?

But then: what to do with global variables that change per page. Simple example: Let's say I have this code in page.php:

<?php include "header.php"; ?>

and header.php looks like this:

<header>
    <h1><?php echo $pageTitle; ?></h1>
</header>

Is it simply as easy to put the value for that page in page.php? Like so:

$pageTitle = "I like bananas";
<?php include "header.php"; ?>

This concerns me, because if you have a lot of these things - PHP turns ugly: after a while your base PHP file (page.php) is cluttered with variables. Not thousands of them, obviously, but I guess it could be around a dozen for my project. Is that a "problem" or is that something that usually happens? If it's not usual, how then should I handle variables in PHP?

And what about functions? For instance, if I want to add a class to a certain item in header.php, if a variable is 5 (obviously the real example is more elaborate), I could write a function like so:

function checkBananaAmount($int) {
    if ($int == 5) echo 'class="eat-bananas"';
}

and call it like so:

<div <?php checkBananaAmount($myBanana); ?>></div>

The question is, would I put this function in a separate file (similar to Wordpress' functions.php, or would I just place it in header.php? If the former, does that mean I have to require "functions.php" in each page with one of these functions as well?

As you can see I am wondering how the basic structure of PHP projects are: how to sort variables and functions, how to keep things neatly organised and so on, without losing productivity and keeping efficiency.

  • 写回答

1条回答 默认 最新

  • dsfgdsjfd78773 2016-04-08 17:25
    关注

    There are many ways to structure projects, including whether or not to include a framework (can take time to learn, but benefits enormously as project grows).

    These days I would not start a php project with a blank slate.

    As a front end person, I assume you know frameworks such as angular or knockout.

    A good starting point might be a simple framework like Slim http://www.slimframework.com/

    And a templating system like Twig http://twig.sensiolabs.org/

    Even if you are going the start from scratch method, you should have a good look at PHP The Right Way http://www.phptherightway.com/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染