访问另一个PHP文件中的类变量

访问另一个PHP文件中的类变量,php,variables,scope,require,Php,Variables,Scope,Require,我想使用另一个php文件中的变量作为类。但我总是得到错误:注意:未定义变量: 第一:我创建一个用户对象: 文件:index.php <?php // include the configs / constants for the db connection require_once("config/config.php"); // load the user class require_once("classes/User.php"); $user = new User(); in

我想使用另一个php文件中的变量作为类。但我总是得到错误:注意:未定义变量:

第一:我创建一个用户对象: 文件:index.php

<?php

// include the configs / constants for the db connection
require_once("config/config.php");

// load the user class
require_once("classes/User.php");

$user = new User();

include("views/order.php");
// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");
文件livesearch.php

require_once("../classes/User.php");

echo $User->color;
echo $User->color;

我在index.php文件中从类user创建了一个对象,在那里我还使用了一个require once来创建user.php文件,它可以正常工作。为什么我不能访问类的变量?

PHP中的变量名区分大小写:

echo $User->color;
// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");
应该是

echo $user->color;
另外,
livesearch.php
无法访问
index.php
中的变量,除非:

// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");
  • 它包含在
    index.php
    中。在这种情况下,它可以访问
    index.php
    中指定的所有变量,然后再将其包括在内
  • // load the user class
    require_once("User.php");
    
    $user = new User();
    
    include("livesearch.php");
    
  • livesearch.php
    包括
    index.php
    。在这种情况下,它可以访问
    index.php
    中包含
    index.php
    之后的
    index.php中分配的所有变量
  • // load the user class
    require_once("User.php");
    
    $user = new User();
    
    include("livesearch.php");
    
例如。 您的文件,但稍有修改:

文件:index.php
// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");
文件:User.php

class User
{
   public $color = "green";
}
class User
{
   public $color = "green";
}
文件:livesearch.php

require_once("../classes/User.php");

echo $User->color;
echo $User->color;
与写作相同:

// From User.php
class User
{
   public $color = "green";
}

// From index.php
$user = new User();

// From livesearch.php
echo $User->color;

PHP中的变量名区分大小写:

echo $User->color;
应该是

echo $user->color;
另外,
livesearch.php
无法访问
index.php
中的变量,除非:

// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");
  • 它包含在
    index.php
    中。在这种情况下,它可以访问
    index.php
    中指定的所有变量,然后再将其包括在内
  • // load the user class
    require_once("User.php");
    
    $user = new User();
    
    include("livesearch.php");
    
  • livesearch.php
    包括
    index.php
    。在这种情况下,它可以访问
    index.php
    中包含
    index.php
    之后的
    index.php中分配的所有变量
  • // load the user class
    require_once("User.php");
    
    $user = new User();
    
    include("livesearch.php");
    
例如。 您的文件,但稍有修改:

文件:index.php
// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");
文件:User.php

class User
{
   public $color = "green";
}
class User
{
   public $color = "green";
}
文件:livesearch.php

require_once("../classes/User.php");

echo $User->color;
echo $User->color;
与写作相同:

// From User.php
class User
{
   public $color = "green";
}

// From index.php
$user = new User();

// From livesearch.php
echo $User->color;

用于文件livesearch.PHP的PHP:

 require_once("../classes/User.php");

 $user = new User;
 echo $user->color;

用于文件livesearch.PHP的PHP:

 require_once("../classes/User.php");

 $user = new User;
 echo $user->color;

您应该使用单例设计模式来提高速度。 在这种情况下,我不推荐这样的用法。(这是->颜色,而不是用户::颜色)

研究设计模式,多态性

答复

  • userclass.php 类用户 { 公共$color=“绿色”; }
$User=新用户

  • livesearch.php 需要_一次(“../classes/User.php”)

echo$User->color

您应该使用单例设计模式来提高速度。 在这种情况下,我不推荐这样的用法。(这是->颜色,而不是用户::颜色)

研究设计模式,多态性

答复

  • userclass.php 类用户 { 公共$color=“绿色”; }
$User=新用户

  • livesearch.php 需要_一次(“../classes/User.php”)

echo$User->color

$User::color
$User->color
表示您事先已经完成了
$User=new User()
。我是在index.php中完成的,并包含了一个视图。加载页面时,index.php会创建一个用户对象,然后显示“真实”页面(视图),为什么liveseach.php会访问您在index.php中创建的对象实例?(或者我错过了一个要求?)livesearch.php需要一些变量来定制sql查询。当用户在输入字段中键入内容时,livesearch需要变量来设置操作系统、comapany等的“where”-过滤器。我们有一个带有软件的数据库,该数据库适用于特定用户或公司。这就是为什么livesearch.php需要一些变量的原因,这些变量是通过index.php中的调用设置的。这只是一个例子,我不想发布我的孔代码,这会破坏页面。
$User::color
$User->color
表示您事先已经完成了
$User=new User()
。我是在index.php中完成的,并包含了一个视图。加载页面时,index.php会创建一个用户对象,然后显示“真实”页面(视图),为什么liveseach.php会访问您在index.php中创建的对象实例?(或者我错过了一个要求?)livesearch.php需要一些变量来定制sql查询。当用户在输入字段中键入内容时,livesearch需要变量来设置操作系统、comapany等的“where”-过滤器。我们有一个带有软件的数据库,该数据库适用于特定用户或公司。这就是为什么livesearch.php需要一些变量的原因,这些变量是通过index.php中的调用设置的。这只是一个例子,我不想发布我的孔代码,这会破坏页面。你不应该使用“单一设计模式的速度”。你应该使用它们,因为它们适合场景。你不应该使用“单一设计模式来提高速度”。您应该使用它们,因为它们适合场景。这意味着,当我包括创建对象的文件时,我只能访问User.php文件中的变量?我想我必须包含User.php文件,因为有声明的变量。当我在livesearch.php中包含index.php时,我可以访问该变量。但这很糟糕,因为index.php包含order.php,这是一个html站点。所以我有一个网站中的一个网站。但我想我明白了。谢谢@阿达姆里弗,我明白了。我想理解的是,我找不到答案:如果你必须包含你想使用这个变量的页面,那么包含10+个文件不是不可行吗?有什么方便的方法可以做到这一点吗?也就是说,当我包括创建对象的文件时,我只能访问User.php文件中的变量?我想我必须包含User.php文件,因为有声明的变量。当我在livesearch.php中包含index.php时,我可以访问该变量。但这很糟糕,因为index.php包含order.php,这是一个html站点。所以我有一个网站中的一个网站。但我想我明白了。谢谢@阿达姆里弗,我明白了。我想了解的是,我找不到答案:如果你有
// load the user class
require_once("User.php");

$user = new User();

include("livesearch.php");