将各种 PHP 文件合并/包含到一个 PHP 文件中

发布于 2024-10-08 22:00:53 字数 1011 浏览 0 评论 0原文

朋友们大家好,我是一名新手程序员,对 PHP 非常陌生。我正在寻求帮助编写一个小型 php 函数。情况是这样的:

我正在运行 Joomla,并且我有一个位于 BLOCKS 中的模板,例如:

header.php
topmodules.php
mainbody.php
bottommodules.php
footer.php

我的所有块都放置在一个目录中(/layouts/blocks)。所有这些块都需要加入到主 Index.php 文件中。

我知道的函数是这样的:

<?php
function get_header(){
  require_once(TEMPLATEPATH.'/layouts/blocks/header.php');
}
?>

然后这样调用它:

<?php get_header(); ?>

但这不是很专业,而且我还必须为每个文件编写一个函数,而且这也可以通过使用来完成

<?php require(YOURBASEPATH . DS .'layouts'. DS .'blocks'. DS . "header.php"); ?>

但我正在寻找的是有一个函数/类,它可以从该目录获取该 PHP 文件,只需传递文件名,以便我将来可以向该目录添加更多块,而无需重新编写该函数,只需像这样调用它们即可

<?php $this->getBlock('header') ?>
<?php $this->getBlock('topmodules') ?>
<?php $this->getBlock('mainbody') ?>
<?php $this->getBlock('bottommodules') ?>
<?php $this->getBlock('footer') ?>

:帮助。

Hello Friends I am a newbie programmer and very very new to PHP. I am seeking help in writing a small php Function. The situation is like this:

I am running Joomla and I have a template which is in BLOCKS for example:

header.php
topmodules.php
mainbody.php
bottommodules.php
footer.php

All my blocks are placed in a directory (/layouts/blocks). All these blocks need to be joined in the main Index.php file.

The function I know is something like this:

<?php
function get_header(){
  require_once(TEMPLATEPATH.'/layouts/blocks/header.php');
}
?>

And then call it like:

<?php get_header(); ?>

But that is not very professional and also I will have to write a function for every file moreover this can also be accomplished by just using

<?php require(YOURBASEPATH . DS .'layouts'. DS .'blocks'. DS . "header.php"); ?>

But what I am looking for is to have a single function /class which can get that PHP file from that directory, just by passing the name of the file so that I can add some more blocks to that directory in future without re-writing the function and simply call them like:

<?php $this->getBlock('header') ?>
<?php $this->getBlock('topmodules') ?>
<?php $this->getBlock('mainbody') ?>
<?php $this->getBlock('bottommodules') ?>
<?php $this->getBlock('footer') ?>

Kindly help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏闹i 2024-10-15 22:00:53

您只需要向您拥有的函数添加一个参数:

function getBlock($filename){
  require_once(YOURBASEPATH . DS . 'layouts' .DS . 'blocks'. DS . $filename .'.php');
}

You just need to add a parameter to the function you have:

function getBlock($filename){
  require_once(YOURBASEPATH . DS . 'layouts' .DS . 'blocks'. DS . $filename .'.php');
}
玻璃人 2024-10-15 22:00:53
function get_header_improved($s){
  require_once(TEMPLATEPATH.'/layouts/blocks/' . $s . '.php');
}

<?php get_header_improved('header') ?>
<?php get_header_improved('topmodules') ?>
<?php get_header_improved('mainbody') ?>
<?php get_header_improved('bottommodules') ?>
<?php get_header_improved('footer') ?>

我没有尝试过,但这应该有效。

function get_header_improved($s){
  require_once(TEMPLATEPATH.'/layouts/blocks/' . $s . '.php');
}

<?php get_header_improved('header') ?>
<?php get_header_improved('topmodules') ?>
<?php get_header_improved('mainbody') ?>
<?php get_header_improved('bottommodules') ?>
<?php get_header_improved('footer') ?>

I not tried, but this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文