English 中文(简体)
具有动态名称空间的申报班
原标题:redeclaring class with dynamic namespace

我试图找到一种办法,通过关闭的方式重新宣布一个阶级。 这是迄今为止的情况:


I have two simple classes which are identical, except for the namespace. The namespace in test2.php is called "bla" and in new.php it s called "newname". Other than that, they are exactly the same.

namespace bla; //test2.php
class A
{
    public function __construct()
    { }

    public function bla()
    {
        echo  [A bla] ;
    }
}

include ("new.php");
include ("test2.php");

use newnameA as A; // <-- dynamic

$a = new A();
$a->bla();

因此,这部法典只是从新法实施。 php 而不是测试2.php的类别。 这实际上是需要的。 但我的问题是守则的最后一部分。 • 通常使用一个名称空间:

$a = new blaA(); //Defined in  test2.php 
$a->bla();

但是,由于将这一条改为:

$a = new A(); //Defined in  test2.php 
$a->bla();

因此,没有“A(A)”的名称空间。

是否有办法保持“A(A)”名称空间(在试验2.php中界定),但它仍然把这一方法从“新的.php”中删除?

问题回答

在此情况下,请每一类人打字。

$bla = new bla/A("");
$bla->bla();        // to call from bla.php

二级

$newname = new newname/A("");
$newname->something(); // to call from newname.php

并且将工作,你不需要使用关键词的超稿。

此外,在一只字典上写课时也好事,如:

namespace bla{
     class A{
        // content
     }
}

namespace newname{
     class A{
        // content
     }
}

正如在评论中所说的那样,这不是一个“封闭”专题,而只是动态名称空间专题。

I don t think you can use tue use keyword dynamically. However, PHP is a dynamic language, you can chose dynamically which class you want with meta-programming:

$wantedClass =  myWantedNamespaceMyWantedClass ;
$instance    = new $wantedClass();




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签