在为网站编写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的每个范围,有哪些优点和缺点?
发布于 2014-04-10 09:00:46
更好的是下模 :^ )
include ("myclass.php");
function x (\MyClass $theClass) {
$theClass->theProperty="foo";
$theClass->doStuff ();
}
function y (\MyClass $theClass) {
$theClass->theProperty="bar";
$theClass->doStuff ();
}但这取决于..。*^)
https://stackoverflow.com/questions/22983321
复制相似问题