将各种 PHP 文件合并/包含到一个 PHP 文件中
朋友们大家好,我是一名新手程序员,对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要向您拥有的函数添加一个参数:
You just need to add a parameter to the function you have:
我没有尝试过,但这应该有效。
I not tried, but this should work.