从服务器上其他地方的脚本实例化 class 或者:访问脚本中的 const 变量
Instantiating a class from a script elsewhere on the server OR: accessing const variables in script
我在从脚本中实例化 class 时遇到问题。我的代码基本上是这样的:
ConstAttributes.php 位于服务器上,例如/var/www/abc/def/
<?php
namespace My\Path;
class ConstAttributes {
const ONE = "some";
const TWO = "text";
const THREE = "here";
}
?>
index.php 位于服务器的其他位置,例如/var/www/xyz/123/
<?php
use My\Path\ConstAttributes;
$aInst = new My\Path\ConstAttributes();
?>
我也试过:
use My\Path\ConstAttributes;
$aInst = new ConstAttributes();
但结果相同。我正在 apache2 服务器上进行现场测试。 apache ist 配置为指向索引页。当我刷新页面时,它只是空白 - 上面什么也没有。创建实例后出现的所有内容根本不显示;好像剧本挂在那里。当我做这样的事情时:
use My\Path\ConstAttributes;
//$aInst = new My\Path\ConstAttributes();
echo 'test';
我确实收到了预期的回显消息。
这样做的关键 是访问 index.php 脚本中的 const
变量。在尝试实例化 class 之前,我尝试了 ConstAttributes::ONE
但那就像我在实例化 class.
时所做的那样死在那里
我已经用谷歌搜索了很多,但无法解决问题。将不胜感激。
提前致谢。
如果您尝试在 php class 中使用常量,php 引擎会抛出异常“注意:使用未定义的常量 ONE - 假定 'ONE'在...”。要解决此问题,可以定义和使用全局常量。请在此处查看演示代码。
//
<?php
/*
* mypath\ConstAttributes.php
*/
namespace MyPath2;
//
define("ONE1", "One1");
const TWO2 = "Two2";
define("SIX", "Six6");
const SEVEN = "Seven7";
define("EIGHT", "Eight8");
const ONE = "some";
const TWO22 = "text2";
define("TWO", "text");
const THREE = "here";
//
/**
* Description of ConstAttributes
*
* @author B
*/
class ConstAttributes {
var $one = ONE;
var $two = TWO;
var $three = THREE;
var $two2 = TWO2;
var $four = "four4";
var $five = "five5";
var $seven = SEVEN ;
var $eight = EIGHT ;
function MyOne(){
return ONE1;
}
function MyTwo(){
return $this->two2;
}
function MyThree(){
return $this->three;
}
function MyFour(){
return $this->four;
}
function MySeven(){
return $this->seven;
}
}
//
完成后,您就可以像往常一样使用 class 和 index.php。
//
<!DOCTYPE html>
<!--
index.php
-->
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<?php
use MyPath2\ConstAttributes;
include 'mypath\ConstAttributes.php';
$aInst = new ConstAttributes();
echo gettype($aInst)."<br>";
echo $aInst->MyOne()."<br>";
echo $aInst->MyTwo()."<br>";
echo $aInst->MyFour()."<br>";
echo $aInst->five."<br>";
echo SIX."<br>";
echo $aInst->MySeven()."<br>";
echo $aInst->eight."<br>";
echo "////////////////////////<br>";
echo $aInst->one."<br>";
echo TWO."<br>";
echo $aInst->MyThree()."<br>";
echo "///////////////////////////<br>";
//echo TWO22."<br>";
echo "///////////////////////////<br>";
?>
</body>
</html>
//
测试结果如下:
//////////////////output////////////
// object
// One1
// Two2
// four4
// five5
// Six6
// Seven7
// Eight8
////////////////////////
// some
// text
// here
///////////////////////////
///////////////////////////
//
尽情享受吧!
我在从脚本中实例化 class 时遇到问题。我的代码基本上是这样的:
ConstAttributes.php 位于服务器上,例如/var/www/abc/def/
<?php
namespace My\Path;
class ConstAttributes {
const ONE = "some";
const TWO = "text";
const THREE = "here";
}
?>
index.php 位于服务器的其他位置,例如/var/www/xyz/123/
<?php
use My\Path\ConstAttributes;
$aInst = new My\Path\ConstAttributes();
?>
我也试过:
use My\Path\ConstAttributes;
$aInst = new ConstAttributes();
但结果相同。我正在 apache2 服务器上进行现场测试。 apache ist 配置为指向索引页。当我刷新页面时,它只是空白 - 上面什么也没有。创建实例后出现的所有内容根本不显示;好像剧本挂在那里。当我做这样的事情时:
use My\Path\ConstAttributes;
//$aInst = new My\Path\ConstAttributes();
echo 'test';
我确实收到了预期的回显消息。
这样做的关键 是访问 index.php 脚本中的 const
变量。在尝试实例化 class 之前,我尝试了 ConstAttributes::ONE
但那就像我在实例化 class.
我已经用谷歌搜索了很多,但无法解决问题。将不胜感激。
提前致谢。
如果您尝试在 php class 中使用常量,php 引擎会抛出异常“注意:使用未定义的常量 ONE - 假定 'ONE'在...”。要解决此问题,可以定义和使用全局常量。请在此处查看演示代码。
//
<?php
/*
* mypath\ConstAttributes.php
*/
namespace MyPath2;
//
define("ONE1", "One1");
const TWO2 = "Two2";
define("SIX", "Six6");
const SEVEN = "Seven7";
define("EIGHT", "Eight8");
const ONE = "some";
const TWO22 = "text2";
define("TWO", "text");
const THREE = "here";
//
/**
* Description of ConstAttributes
*
* @author B
*/
class ConstAttributes {
var $one = ONE;
var $two = TWO;
var $three = THREE;
var $two2 = TWO2;
var $four = "four4";
var $five = "five5";
var $seven = SEVEN ;
var $eight = EIGHT ;
function MyOne(){
return ONE1;
}
function MyTwo(){
return $this->two2;
}
function MyThree(){
return $this->three;
}
function MyFour(){
return $this->four;
}
function MySeven(){
return $this->seven;
}
}
//
完成后,您就可以像往常一样使用 class 和 index.php。
//
<!DOCTYPE html>
<!--
index.php
-->
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<?php
use MyPath2\ConstAttributes;
include 'mypath\ConstAttributes.php';
$aInst = new ConstAttributes();
echo gettype($aInst)."<br>";
echo $aInst->MyOne()."<br>";
echo $aInst->MyTwo()."<br>";
echo $aInst->MyFour()."<br>";
echo $aInst->five."<br>";
echo SIX."<br>";
echo $aInst->MySeven()."<br>";
echo $aInst->eight."<br>";
echo "////////////////////////<br>";
echo $aInst->one."<br>";
echo TWO."<br>";
echo $aInst->MyThree()."<br>";
echo "///////////////////////////<br>";
//echo TWO22."<br>";
echo "///////////////////////////<br>";
?>
</body>
</html>
//
测试结果如下:
//////////////////output////////////
// object
// One1
// Two2
// four4
// five5
// Six6
// Seven7
// Eight8
////////////////////////
// some
// text
// here
///////////////////////////
///////////////////////////
//
尽情享受吧!