【发布时间】:2014-04-10 08:58:55
【问题描述】:
在为网站编写 PHP 代码时,什么更好:
<?php
include ("myclass.php");
$theClass = new MyClass();
function x () {
global $theClass;
$theClass->theProperty="foo";
$theClass->doStuff();
}
function y () {
global $theClass;
$theClass->theProperty="bar";
$theClass->doStuff();form
}
?>
或:
<?php
include ("myclass.php");
function x () {
$theClass = new MyClass();
$theClass->theProperty="foo";
$theClass->doStuff ();
}
function y () {
$theClass = new MyClass();
$theClass->theProperty="bar";
$theClass->doStuff ();
}
?>
对于$theClass的每个范围,上述哪一项更可取,如果有的话,有哪些优点和缺点?
【问题讨论】: