[英]Add Object ( Class ) a array in PHP, does not recognize the type of object
I've the next classes and interfaces 我的下一个类和接口
namespace includ\views;
interface IView
{
public function getContenido( );
public function imprimirContenido( );
}
namespace includ\views;
require_once ('IView.php' );
use includ\views\IView as IView;
interface IMenu extends IView
{
public function getTipo();
public function __clone();
}
namespace includ\views;
require ('IMenu.php');
use includ\views\IMenu;
class MenuAdministrador implements IMenu
{
...
}
and make a array 然后做一个数组
require_once ( 'menu/MenuAdministrador.php' );
$menus=array();
$menus[] = new includ\views\MenuAdministrador();
//or other way
//array_push( $menus, new includ\views\MenuAdministrador() );
but, Im try to check the type of class, I make some like : 但是,我试图检查类的类型,我做了一些像:
var_dump($menus[0] instanceof IMenu );
the answer is: 答案是:
bool(false)
which is the best way to add a Object (class ) to array? 哪一个是将Object(class)添加到数组的最佳方法?
It's a namespace problem. 这是一个名称空间问题。 You have to use Includ\\views\\IMenu
first or better use the fully qualified class name with instanceof
. 您必须首先use Includ\\views\\IMenu
或更好地将全限定类名与instanceof
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.