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/

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题