PHP中的变量类扩展 - 是可能吗? [英] Variable variable class extensions in PHP--is it possible?

查看:121
本文介绍了PHP中的变量类扩展 - 是可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中可能有以下类似的情况?

  $ blah ='foo1'; 

class foo2 extends $ blah {
// ...
}

class foo1 {
// ...
}

这会产生错误。



我想动态设置$ blah,所以我可以扩展任何我想要的类。



编辑:我想在相关类中使用另一个类的函数。最后它会是这样的:

 最后扩展foo1 extends foo2 extends foo3 extends foo4 extends parent {...} 

最后我决定在类中实例化另一个类并使用它。不是最好的选择,因为他们你两个相同的类,但这不会经常使用,所以它会工作,现在。

解决方案

你假设这里的PHP执行从上到下,

 <?php 
foo(); #works

function foo(){
printbar;
}






 <?php 

foo(); #dies

if($ i == 1)
{
function foo(){
printbar;
}
}






 <?php 
$ i = 1;
if($ i == 1)
{
function foo(){
printbar;
}
}

foo(); #works






现在,虽然您可以有条件地创建类:

 <?php 

class A {}
class B {}
if(false){
class C extends B {
public static function bar(){
printbaz;
}
}
}
C :: bar(); #dies

您无法在运行时从变量实例化一个:

 <?php 
class A {}
class B {}
$ x ='B'
if(false){
class C extends $ x {
public static function bar(){
printbaz;
}
}
}
C :: bar();
--->解析错误:语法错误,意外的T_VARIABLE,期望第7行的/tmp/eg.php中的T_STRING

有一种方法可以用Eval做,但你真的不想去那里:

 <?php 

class A {}
class B {}
$ x ='B';
if(true){
$ code =<<< EOF
class C extends $ x {
public static function bar(){
print baz;
}
}
EOF;

eval($ code);
}
C :: bar();
$ o = new C;
if($ o instanceof $ x)
{
printWIN!\\\
;
}
---> barWIN!但是,这里有一个更重要的问题:












任何使用代码的人都可以在



(或者,如果你在鞭打,做这个eval技巧)

/ p>

Is something like the following possible in PHP?

$blah = 'foo1';

class foo2 extends $blah {
    //...
}

class foo1 {
    //...
}

This gives an error.

I want to dynamically set $blah so I can extend whatever class I want.

Edit: The reason for wanting to do this because I wanted to use a function out of another class in a related class. In the end it would have been something like:

Final extends foo1 extends foo2 extends foo3 extends foo4 extends parent { ... }

In the end I decided to instantiate the other class within the class and use it. Not the best options because they both you 2 of the same classes, but this won't be used that often, so it will work for now.

解决方案

You're assuming here php executes top to bottom, but it doesn't quite work like that:

<?php
foo();  # works

function foo(){
  print "bar";
}


<?php

foo();  #dies

if( $i == 1 )
{
  function foo(){
    print "bar";
  }
}


<?php
$i = 1;
if( $i == 1 )
{
  function foo(){
    print "bar";
  }
}

foo(); #works


Now, although you can conditionally create classes:

<?php

class A { }
class B { }
if( false ){ 
  class C extends B { 
    public static function bar(){
      print "baz"; 
    }
  }
}
C::bar(); # dies

You cant instantiate one at runtime from a variable:

<?php
class A { }
class B { }
$x = 'B'; 
if( false ){ 
  class C extends $x { 
    public static function bar(){
      print "baz"; 
    }
  }
}
C::bar();
---> Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /tmp/eg.php on line 7

There is a way to do it with Eval, but you really don't want to go there:

<?php

class A { }
class B { }
$x = 'B'; 
if( true ){ 
 $code =<<<EOF
  class C extends $x { 
    public static function bar(){
      print "baz"; 
    }
  }
EOF;

  eval( $code );
}
C::bar();
$o = new C; 
if ( $o instanceof $x )
{
  print "WIN!\n";
}
--->barWIN!

However, there is a more important question here:

Why the hell would you want to extend a different class at runtime

Anybody using your code will want to hold you down and whip you for that.

( Alternatively, if you're into whipping, do that eval trick )

这篇关于PHP中的变量类扩展 - 是可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆