Php包含类中的属性

时间:2016-03-03 22:30:53

标签: php class methods properties

我在调用类中包含的变量时遇到问题。我在这里错过了什么?提前感谢您的支持。非常感谢。

//index2.php
$var2 = 'var2';



//index.php
class something {
  __construct() {
    include('index2.php');    
  }
}


$run_it = new something();

echo $run_it->var2; //how do I call var2?

5 个答案:

答案 0 :(得分:0)

您无法使用包含文件扩展类的属性。该类定义为编写。您可以通过以下方式动态地向代码添加代码。

  1. 扩展课程。
  2. 使用特征(将粘贴函数复制到类中)
  3. 使用magic methods(__ call,__ get,__ set)
  4. 此示例将根据您的需要打印在全局范围(如您的示例)中定义的变量。 但是,这不是应该使用的方式,我建议找一种不同的写作课程的方法。

    include 'somefile.php'; // <?php $var3 = 'testing' ?>
    
    class foo{
      public $var2 = 'var2'; # should be declared within the class.
    
      public function __get($name){
        // property 'var3' is not defined in the class so this method is magically called instead.
        if(isset($GLOBALS[$name])){
          return $GLOBALS[$name]; // Here we return the value $var3 defined in the global scope.
        } else {
          trigger_error("Call to undefined variable '$name'");
        }
      }
    
      public function __construct(){ # Missed function statement.
        echo 'hello';
      }
    }
    
    $c = new foo();
    echo $c->var3; // testing
    

答案 1 :(得分:0)

如果您的1包含此内容:

index2.php

然后,您可以包含该文件以访问$var2 = 'var2'; 。然后,您可以将该变量作为参数传递给$var2构造函数:

something

很明显还有其他方法可以做你正在尝试做的事情(通常有多种方法可以做大多数事情),但实际上将外部数据输入对象的最明智的方法是通过它通过构造函数参数。

如果不这样做,该类将永远依赖于存在于相对于类或调用文件的正确位置的其他文件,并在其中设置所有正确的变量。如果该文件被更改或移动,则该类的对象如果甚至可以实例化则可能无法正常运行。

答案 2 :(得分:0)

你可以这样做。有点奇怪的做事方式,但这里有:

的index.php

<?php
class something{
  function __construct() {
    include('index2.php');    
  }
}

//s1
$s1 = new something();
$s1->var2 = 'var2 changed';

//s2
$s2 = new something();

//print em out
print_r($s1);
print_r($s2);

index2.php

<?php
    $this->var2 = 'var2'; //$this pseudo-variable works because we're including index2.php within the scope of our class instance
?>

输出 $ php a.php

something Object
(
    [var2] => var2 changed
)
something Object
(
    [var2] => var2
)

答案 3 :(得分:0)

我同意Xorifelse,你可以用特质做到这一点。示例

fields.php

trait Aa {

public $fields = [
  "name"=>"string->5",
  "number"=>"integer->3",
  ];
}

traits.php

include('fields.php');

class ABC {

use Aa;

public function __set($field,$value)
{
  if(array_key_exists($field,$this->fields))
  {
    $this->fields[$field] = $value;
   }
   else
   {
   $this->fields[$field] = null;
   }
}

public function __get($field)
{
  if(array_key_exists($field,$this->fields))
  {
   return $this->fields[$field];
  }
  else
  {
  return null;
  }
}
}

$ a =新ABC();

$ a-&gt; number = 111;

的var_dump($ A-&GT;字段);

array(2) {
["name"]=>
string(9) "string->5"
["number"]=>
int(111)
}

的var_dump($ A-&GT;数);

int(111)

答案 4 :(得分:0)

您可以利用get_defined_vars。假设您的变量位于文件中(例如filevars.php):

<强> filevars.php

<?php
$name = 'myName';
$address = 'myAddress';
$country = 'myCountry';
$phones = array('123456789','987654321');
?>

您可以向类(getVars)添加方法,并定义包含vars中存在的所有变量的属性(filevars.php):

<强>类test.php的

<?php
class Test
{
    public $vars;

    function getVars()
    {
        include ('filevars.php');
        $this->vars = get_defined_vars();
    }
}
?>

<强>用法

<?php
include ('class-test.php');
$myClass = new Test();
$myClass->getVars();
print_r($myClass->vars);
?>

<强>输出:

Array
{
    [name] => myName
    [address] => myAddress
    [country] => myCountry
    [phones] => Array
    {
        [0] => 123456789
        [1] => 987654321
    }
}

您还可以引用任何包含的变量,例如: echo $myClass->vars['country']; // myCountry

相关问题