Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在类中将$SEVER[';DOCUMENT#u ROOT';]设置为属性?_Php_Oop - Fatal编程技术网

Php 如何在类中将$SEVER[';DOCUMENT#u ROOT';]设置为属性?

Php 如何在类中将$SEVER[';DOCUMENT#u ROOT';]设置为属性?,php,oop,Php,Oop,我正在尝试使用$\u SERVER['DOCUMENT\u ROOT']为类设置属性 不幸的是,它不能正常工作。下面是我设置它的方式 <?php /* Load 404 page if file is accessed directly */ if(!defined('INCLUDE_CHECK')) die(header("HTTP/1.0 404 Not Found")); class VM_Definitions { private $root; public

我正在尝试使用
$\u SERVER['DOCUMENT\u ROOT']
为类设置属性

不幸的是,它不能正常工作。下面是我设置它的方式

<?php

/* Load 404 page if file is accessed directly */
if(!defined('INCLUDE_CHECK')) die(header("HTTP/1.0 404 Not Found"));

class VM_Definitions
{
    private $root;

    public function __construct()
    {
        $this->$root = $_SERVER['DOCUMENT_ROOT'];
    }
}
$test = new VM_Definitions;
echo $test->root;

?>

一旦声明属性:

您需要将其设置为:

$this->root = $_SERVER['DOCUMENT_ROOT'];
     ^ no more `$`
然后,如果要直接访问该属性,则需要将可见性设置为
public

或者,如果要将其保留为
私有
,请设置一个getter方法:

public function getRoot()
{
    return $this->root;
}

$test = new VM_Definitions;
echo $test->getRoot();

申报财产后:

您需要将其设置为:

$this->root = $_SERVER['DOCUMENT_ROOT'];
     ^ no more `$`
然后,如果要直接访问该属性,则需要将可见性设置为
public

或者,如果要将其保留为
私有
,请设置一个getter方法:

public function getRoot()
{
    return $this->root;
}

$test = new VM_Definitions;
echo $test->getRoot();

这是我在输入此问题时的输入错误。您正在尝试访问函数外部的
private
属性-这是您收到的错误。这是我在输入此问题时的输入错误。您正在尝试访问函数外部的
private
属性-这是您收到的错误