我不知所措。我已经花了几个小时广泛地浏览了我的两个课程。没有什么是静态的,没有任何静态引用,但我无法摆脱这个错误。
A类文件( ClassA.php )
<?php
namespace MyProject\A;
require_once "B.php";
use MyProject\B as B;
class A
{
private $privateVariable;
public function __construct()
{
$b = new B\B();
$this->privateVariable = $b->something;
}
}
B类文件( B.php )
<?php
namespace MyProject\B;
class B
{
public $something;
public function __construct()
{
$this->something = "Some (dynamic) string value";
}
}
我不是新手,但我之前从未遇到过这个问题(至少没有静态变量或方法或静态引用)。
<小时/> 我得到的错误是:
严格标准:在A.php中在线访问静态属性MyProject \ B \ B :: $ something为非静态(此行:
$this->privateVariable = $b->something
)
和
注意:未定义的属性:MyProject \ B \ B :: $中的某些东西在线上(此行:
$this->privateVariable = $b->something
)
我认为任何阅读此内容的人都明白我希望将B的变量$something
变为A {'变量$privateVariable
,同时在实例化时将B类保持为A类中的实例。 A类如:
<?php
namespace MyProject\Something;
require_once "A.php";
use MyProject\A as A;
$a = new A\A()
// $a's private variable $privateVariable should be set to "Some (dynamic) string value"
非常感谢有关此问题的任何帮助!
<小时/> 的修改use AS
更改为use as
并更改使用方式调用相应的类别
我为这个问题道歉,但我认为这更像是一个错误而不是实际的代码问题。我只是责怪Godaddy这个(是的,我知道Godaddy是很好的选择讽刺)
答案 0 :(得分:0)
有一个值得注意的观察结果:你不能写use ... AS ...
你写use ... as ...
。 AS应该是所有小写的。因此;正确的use statement
应为:use MyProject\ClassB as cB
。下面的代码摘录将说明这些要点。
<?php
namespace MyProject;
require_once "ClassB.php";
use MyProject\B as cB;
class A {
private $privateVariable;
public function __construct() {
$b = new cB();
$this->privateVariable = $b->getSomething();
}
}
?>
<?php
namespace MyProject;
class B{
public $something;
public function __construct(){
$this->something = "Some (dynamic) string value";
}
// YOU MAY WANT TO CREATE A FUNCTION TO GET THE SOMETHING
public function getSomething(){
return $this->something;
}
}
或使用别名....
<?php
namespace MyProject\ClassA;
require_once "ClassB.php";
use MyProject\ClassB as cB;
class A {
private $privateVariable;
public function __construct(){
$b = new cB\B();
$this->privateVariable = $b->getSomething();
}
}
?>
<?php
namespace MyProject\ClassB;
class B{
public $something;
public function __construct(){
$this->something = "Some (dynamic) string value";
}
// YOU MAY WANT TO CREATE A FUNCTION TO GET THE SOMETHING
public function getSomething(){
return $this->something;
}
}
运行代码(第一个示例 - 没有别名):::
<?php
namespace MyProject\Something;
use MyProject\A as A;
require_once "A.php";
require_once "B.php";
$a = new A();
var_dump($a);
// PRODUCES:::
object(MyProject\A)[1]
private 'privateVariable' => string 'Some (dynamic) string value' (length=27)
自己测试HERE。
答案 1 :(得分:0)
这里,完整工作的单个文件示例:
using Plots
gr(reuse=true)
p =plot([0;.1],[0;.2])
gui()
for i=2:10
push!(p,i*.1,randn())
gui()
sleep(.1) # To slow things down for show.
end
测试它。
更新
您使用:
namespace MyProject\ClassA {
use MyProject\ClassB\B;
class A {
private $privateVariable;
public function __construct() {
$b = new B();
$this->privateVariable = $b->something;
print $this->privateVariable;
}
}
}
namespace MyProject\ClassB{
class B{
public $something;
public function __construct()
{
$this->something = "Some (dynamic) string value";
}
}
}
namespace MyProject\Something {
use MyProject\ClassA\A;
$a = new A();
}
但 use MyProject\A as A;
$a = new A\A();
指向命名空间而不是类。
所以这是对的:
use MyProject\A
另一个注意事项是:use MyProject\A\A as A;
$a = new A();
首先使用use \MyProject\A\A
。
答案 2 :(得分:0)
我认为您的命名空间不正确。我的赌注是PHP无法实例化该类并假设您静态调用它。我清理了名称空间并创建了一个巨大的文件it runs fine
namespace MyProject\ClassB;
class B
{
public $something;
public function __construct()
{
$this->something = "Some (dynamic) string value";
}
public function getStr() {
echo $this->something;
}
}
namespace MyProject\ClassA;
use \MyProject\ClassB\B as B;
class A
{
private $privateVariable;
public function __construct()
{
$b = new B();
$this->privateVariable = $b->something;
$b->getStr();
}
}
namespace MyProject\Something;
use \MyProject\ClassA\A as A;
$a = new A();