PHP:这是使用类下定义的函数的正确方法吗?


PHP: Is this the correct way of using a function that is defined under a class?

我的Wordpress插件中有一个PHP类,它在网站中被激活。我想在另一个插件(也在同一网站中激活)中使用在此类中定义的方法(doc_export函数)。我添加了这个,它似乎有效:

<?php
        // Export doc
        if (defined('DOC_ADMIN_MODE')) {
            $my_Documentation = new Class_Documentation;
            $mydocexport = $my_Documentation->doc_export();
            $mydocexport_file = ABSPATH . '_export/' . basename($site['site_url']) . '/doc.xml';
            file_put_contents($mydocexport_file, $mydocexport);
        }
?>

我想知道这是否是正确的方法。谢谢。

是的,这是正确的方法。您还可以在 http://php.net/manual/en/language.oop5.basic.php 中阅读有关 PHP 中的 OOP 基础的信息