php 如何“回声”一个班级?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1619483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:17:18  来源:igfitidea点击:

How to "echo" a class?

phptostring

提问by Andrew

This is probably really easy but I can't seem to figure out how to print/echo a class so I can find out some details about it.

这可能真的很容易,但我似乎无法弄清楚如何打印/回显类,因此我可以找到有关它的一些详细信息。

I know this doesn't work, but this is what I'm trying to do:

我知道这行不通,但这就是我想要做的:

<?php echo $class; ?>

What is the correct way to achieve something like this?

实现这样的目标的正确方法是什么?

回答by David Snabel-Caunt

You could try adding a toStringmethod to your class. You can then echo some useful information, or call a render method to generate HTML or something!

您可以尝试在您的类中添加一个toString方法。然后就可以回显一些有用的信息,或者调用一个render方法来生成HTML什么的!

The __toString method is called when you do something like the following:

__toString 方法在您执行以下操作时被调用:

echo $class;

or

或者

$str = (string)$class;

The example linked is as follows:

链接的例子如下:

<?php
// Declare a simple class
class TestClass
{
    public $foo;

    public function __construct($foo) {
        $this->foo = $foo;
    }

    public function __toString() {
        return $this->foo;
    }
}

$class = new TestClass('Hello');
echo $class;
?>

回答by James McNellis

If you just want to print the contents of the class for debugging purposes, use print_ror var_dump.

如果您只想打印类的内容以进行调试,请使用print_rvar_dump

回答by Luc M

Use var_dump on an instance of your class.

在类的实例上使用 var_dump。

<?php
$my_class = new SomeClass();
var_dump( $my_class );
?>

回答by user24601

To get more detailed info out of your class (if you want to know what's available to a child class for example), you can add a debug()method.

要从您的类中获取更详细的信息(例如,如果您想知道子类可以使用什么),您可以添加一个debug()方法。

Here's an example class with such a method that I use that prints out the methods, default vars, and instance vars in a nice structured way:

这是一个示例类,我使用它以一种很好的结构化方式打印出方法、默认变量和实例变量:

<?php
class TestClass{
    private $privateVar = 'Default private';
    protected $protectedVar = 'Default protected';
    public $publicVar = 'Default public';

    public function __construct(){
        $this->privateVar = 'parent instance';
    }
    public function test(){}
    /**
     * Prints out detailed info of the class and instance.
     */
    public function debug(){
        $class = __CLASS__;
        echo "<pre>$class Methods:\n";
        var_dump(get_class_methods($class));
        echo "\n\n$class Default Vars:\n";
        var_dump(get_class_vars($class));
        echo "\n\n$class Current Vars:\n";
        var_dump($this);
        echo "</pre>";
    }
}

class TestClassChild extends TestClass{
    public function __construct(){
        $this->privateVar = 'child instance';
    }
}

$test = new TestClass();
$test2 = new TestClassChild();

$test->debug();
$test2->debug();

回答by Alexander Sholk

You can use Symfony VarDumper Component http://symfony.com/doc/current/components/var_dumper/introduction.html:

您可以使用 Symfony VarDumper 组件http://symfony.com/doc/current/components/var_dumper/introduction.html

Install it via Composer:

通过 Composer 安装:

composer require symfony/var-dumper 

Usage:

用法:

require __DIR__.'/vendor/autoload.php';

// create a variable, which could be anything!
$someVar = ...;

dump($someVar);