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 当需要通过另一个类方法进行分页时,函数变量不可用_Php_Oop - Fatal编程技术网

Php 当需要通过另一个类方法进行分页时,函数变量不可用

Php 当需要通过另一个类方法进行分页时,函数变量不可用,php,oop,Php,Oop,我希望能够要求页面通过类方法。问题是,当我通过普通的require语句要求页面时,所有变量都可以在要求的页面上访问,但是如果我使用另一个类要求该页面,变量就不再可以访问 我有两个页面,index.php和other.php index.php: class Myclass { public function test(){ $test='hello World'; $other=new other; $other->other_pag

我希望能够要求页面通过类方法。问题是,当我通过普通的require语句要求页面时,所有变量都可以在要求的页面上访问,但是如果我使用另一个类要求该页面,变量就不再可以访问

我有两个页面,index.php和other.php

index.php:
class Myclass {
    public function test(){
        $test='hello World';
        $other=new other;
        $other->other_page('other.php'); //if I change this line to require 'other.php' variables are accessible.
    }
}


class other {
    public function other_page($page){
        require $page;
    }
}

$class=new Myclass;
$class->test();
other.php

<?php
echo $test;

一切正常。我可以将这些变量传递给

other::other_page()
在这两种情况下,一切都是一样的。那个么,为什么类的行为不同呢?除了将变量传递给其他对象之外,还有其他方法使变量可以访问吗

other::other_page()

我将给你们一个简单的例子,从中你们可以理解访问机制

first.php

<?php    
class first {    
    private $name;
    function __construct() {
        $this->name="name";
    }

    function  changeName(){
        $this->name="newName";
    }

    function getName()
    {
        return $this->name;
    }    
}    
?>

second.php

<?php
    include 'first.php';
    $firstObject=new first;
    echo $firstObject->getName().'<br>';
    $firstObject->changeName();
    echo $firstObject->getName();

?>

在您的情况下,如果使用
require'other.php'
,则该内容包含在
Myclass
类的
test()函数中。在该位置定义了
$test


但是当您使用
$other->other_页面('other.php')则该内容包含在
other
类的
other\u页面($page)
函数中。在该位置,
$test
未定义

如有疑问,请参阅

大多数情况下,所有PHP变量都只有一个作用域。这个单一作用域也跨越了包含的和必需的文件。。。但是,在用户定义的函数中引入了局部函数作用域。默认情况下,函数中使用的任何变量都限制在局部函数范围内

包含文件时,它包含的代码将继承包含文件所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局作用域

问题是脚本
other.php
继承了
other\u page()
方法的作用域。此方法内部没有定义局部变量
$test
。这就是你的错误来源。现在,在
MyClass
es
test()
方法中使用直接require应该是合乎逻辑的。包含的文件继承了方法的作用域,在它的作用域中确实有一个
$test
变量

这是普遍的问题。我建议你重新考虑你的设计。不要依赖随机继承某个作用域,而是显式地将该作用域传递给要包含的脚本

<?php

// Controller.php
class Controller {

    public function test()
    {
        $test = 'Hello World';
        // You would probably want to pass that via Dependency-Injection and
        // not as a hardcoded dependecny
        $retriever = new PageRetriever;

        // Only the contents of the second argument are passed
        // to the getPage() method
        echo $retriever->getPage('page.php', ['test' => $test]);
    }

}

// PageRetriever.php
class PageRetriever {

    public function getPage( $page, $args = [] )
    {
        $contents = null;

        // Extract the contents of the $args array into
        // the methods local scope
        extract($args);

        // Initiate output buffering, so that the contents
        // of the script is not immediately displayed
        ob_start();
        require $page;
        $contents = ob_get_contents();
        ob_end_clean();

        return $contents;
    }

}

rehman我了解问题和访问机制。我想知道为什么我的代码在通过类调用包含页面时不起作用,但在需要时直接起作用。@MayankKumar-Answer-edited。请读最后两段,现在我明白了。谢谢谢谢你的帮助。我正在开发一个MVC框架,主要是为了学习,你能给我一些提示吗?以后我一定会看的!:)
<?php
    include 'first.php';
    $firstObject=new first;
    echo $firstObject->getName().'<br>';
    $firstObject->changeName();
    echo $firstObject->getName();

?>
<?php

// Controller.php
class Controller {

    public function test()
    {
        $test = 'Hello World';
        // You would probably want to pass that via Dependency-Injection and
        // not as a hardcoded dependecny
        $retriever = new PageRetriever;

        // Only the contents of the second argument are passed
        // to the getPage() method
        echo $retriever->getPage('page.php', ['test' => $test]);
    }

}

// PageRetriever.php
class PageRetriever {

    public function getPage( $page, $args = [] )
    {
        $contents = null;

        // Extract the contents of the $args array into
        // the methods local scope
        extract($args);

        // Initiate output buffering, so that the contents
        // of the script is not immediately displayed
        ob_start();
        require $page;
        $contents = ob_get_contents();
        ob_end_clean();

        return $contents;
    }

}