自学PHP
过程中遇到一些问题,可能问题会有常识性的错误,请各位见谅!谢谢!
如题,有以下测试用例a.php
<?php
$msg = 'hello';
include 'b.php';
?>
b.php
<?php
echo $msg; // 输出 hello
?>
但是这样的话, 在Zend Studio中会有Undefined variable '$msg'
的警告,虽然不影响运行结果,但是对于后期维护或者其他人的阅读会有影响,所以我想问问这有什么好的方法可以解决这种"强迫症"问题?可以让Zend Studio不报这个警告?
看typecho
源码过程中看到如下语句,位于/usr/themes/default/404.php
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('header.php'); ?>
<div class="col-mb-12 col-tb-8 col-tb-offset-2">
<div class="error-page">
<h2 class="post-title">404 - <?php _e('页面没找到'); ?></h2>
<p><?php _e('你想查看的页面已被转移或删除了, 要不要搜索看看: '); ?></p>
<form method="post">
<p><input type="text" name="s" class="text" autofocus /></p>
<p><button type="submit" class="submit"><?php _e('搜索'); ?></button></p>
</form>
</div>
</div><!-- end #content-->
<?php $this->need('footer.php'); ?>
那typecho
是如何实现使用$this
访问need
的呢?
我注意到typecho
的主题模板中都是使用的$this
来使用提供的插件或者功能的,这又是什么原理呢?
不要使用裸的全局变量,把它包装到某个class里面。
模板是被某个对象的方法include的,其作用域内有
$this
这个变量。