同级目录下有两个php文件
问题:
我如何在indexb.php中的class c中分别调用index.php中class a 和 class b的静态方法?
namespace 和 use 该怎么填?
文件一:index.php
<code>namespace {
use
class a{
static public function speak($a)
{
echo $a;
}
}
}
namespace {
use
class a{
static public function speak($a)
{
echo $a.$a;
}
}
}
</code>文件二:indexb.php
<code>namespace Php {
class c
{
}
}</code>同级目录下有两个php文件
问题:
我如何在indexb.php中的class c中分别调用index.php中class a 和 class b的静态方法?
namespace 和 use 该怎么填?
文件一:index.php
<code>namespace {
use
class a{
static public function speak($a)
{
echo $a;
}
}
}
namespace {
use
class a{
static public function speak($a)
{
echo $a.$a;
}
}
}
</code>文件二:indexb.php
<code>namespace Php {
class c
{
}
}</code>
文件index.php:
<code><?php
namespace A{
class A{
static public function speak($a)
{
echo $a;
}
}
}
namespace B{
class B{
static public function speak($a)
{
echo $a;
}
}
}</code>文件indexb.php
<code><?php
namespace PHP{
use A\A;
use B\B;
class C{
public static function test(){
include "index.php";
A::speak("I am A!");
B::speak("I am B!");
}
}
//测试
\PHP\C::test();
}</code>运行indexb.php 结果I am A!I am B!
是不是你想要的结果?
index.php
<code><?php
namespace test{
class a
{
static public function speak($a)
{
echo $a;
}
}
}
namespace test2{
class b
{
static public function speak($a)
{
echo $a.$a;
}
}
}</code>indexb.php
<code><?php
namespace testt{
include 'index.php';
use test\a;
use test2\b;
class c
{
public $a;
public function speak()
{
// var_dump(new a);
// \test\a::speak($this->a);
a::speak($this->a);
// \test2\b::speak($this->a);
b::speak($this->a);
}
}
$c = new \testt\c();
$c->a = 'zhansan';
$c->speak();
}</code>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号