如何“回响”一个班?

发布于 2024-08-09 00:51:47 字数 156 浏览 3 评论 0 原文

这可能真的很容易,但我似乎不知道如何打印/回显课程,因此我可以找到有关它的一些详细信息。

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

<?php echo $class; ?>

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

反差帅 2024-08-16 00:51:48

您可以尝试添加 toString 你的类的方法。然后你可以回显一些有用的信息,或者调用渲染方法来生成 HTML 之类的!

当您执行以下操作时,将调用 __toString 方法:

echo $class;

$str = (string)$class;

链接的示例如下:

<?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;
?>

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

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

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;
?>
飞烟轻若梦 2024-08-16 00:51:48

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

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

孤星 2024-08-16 00:51:48

在类的实例上使用 var_dump。

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

Use var_dump on an instance of your class.

<?php
$my_class = new SomeClass();
var_dump( $my_class );
?>
征﹌骨岁月お 2024-08-16 00:51:48

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

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

<?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();

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.

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();
一桥轻雨一伞开 2024-08-16 00:51:48

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

通过 Composer 安装:

composer require symfony/var-dumper 

用法:

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

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

dump($someVar);

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

Install it via Composer:

composer require symfony/var-dumper 

Usage:

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

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

dump($someVar);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文