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_Class - Fatal编程技术网

PHP方法/函数如何在类对象中工作

PHP方法/函数如何在类对象中工作,php,oop,class,Php,Oop,Class,我在Wordpress环境中创建了一个主题类 class Theme { function __construct() { add_action('after_setup_theme', array(&$this, 'do_this')); } function do_this() { require_once('helper_functions.php'); } } $theme = new Theme();

我在Wordpress环境中创建了一个主题类

class Theme {
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       require_once('helper_functions.php');
    }
}
$theme = new Theme();
在helper_functions.php中,我有:

function get_image()
{
    return 'working';
}
但现在我很困惑,因为当我执行这个

echo $theme->get_image();
它不起作用……但如果我直接调用它,它的工作原理如下:

echo get_image();

但是我想既然我使用的是类方法,我就需要使用类对象来访问类方法……为什么我可以直接调用它呢?

很明显,您理解它的方式是错误的。在这种情况下,get_image不是一种方法。这只是脚本执行期间可以调用的另一个普通函数

要使它成为一个方法,必须在类声明中声明它

class Theme 
{
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       require_once('helper_functions.php');
    }

    function get_image()
    {
        return 'working';
    }
}
阅读有关对象和类的更多信息。

函数get\u image不是类主题中的函数集,而是在类中包含的单独文件中设置的

如果希望它是类的函数,那么需要将其写入类文件中,将代码移动到类中

您也可以使用类扩展

class Helper_functions  {

    public function get_image() {
        return "working!";
    }
}
并将主题类文件更改为

class Theme extends Helper_functions {

    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'get_image'));
    }

}

$theme = new Theme();
另类 既然你说它是几个文件中的几个函数,你可以在主题类文件或扩展类文件中这样做

class Theme {

    ...

    function get_image() { include('theme_file_get_image.php'); }
    function another_function { include('theme_file_another_function.php'); }
}
中的答案总结了您在这里看到的结果:

包含文件中的代码在与函数相同的范围内执行,定义的函数和类是全局的,而不是插入到其中,以替换其他函数和类

因此,包含文件中的函数是在全局范围内定义的,这就是调用get_image的原因

这应相当于:

//Include the helper functions
require_once('helper_functions.php');

class Theme 
{
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       get_image();
    }
}

$test = new Theme();
echo $test->do_this(); //'working';
public function do_this() {
    function get_image() { ... }
}

请注意,get_image位于全局分数中,而不是主题类的成员函数。

包含和要求通常被认为类似于将文件内容复制/粘贴到父脚本中,这似乎是您所期望的。然而,这并不是真的正确,这就是为什么你所做的不起作用


有些语言有编译器宏,在解释代码之前进行文本替换,因此这将导致wpork。但是php没有,所有的include语句都是在运行时计算的。在您的例子中,在require语句执行之前,类已经完全定义好了。因此,您只是在执行定义全局函数的代码。

您不能拆分类定义。所有这些都需要一次性完成。如果你的班级太大,也许它有太多的责任

将业务逻辑与工厂逻辑分开工厂逻辑是实例化新对象的逻辑,业务逻辑就是这些对象。 按函数分隔类。 使用继承和多态性。 如果在函数中包含文件,则该文件仅存在于该函数的作用域中。意思是你所做的等同于:

//Include the helper functions
require_once('helper_functions.php');

class Theme 
{
    function __construct()
    {
       add_action('after_setup_theme', array(&$this, 'do_this'));
    }

    function do_this()
    {
       get_image();
    }
}

$test = new Theme();
echo $test->do_this(); //'working';
public function do_this() {
    function get_image() { ... }
}

这真的没什么用。

-好吧,也许你能帮上忙。我之所以这样做,是因为我有很多函数要作为一个方法包含,我想把它们组织成单独的文件,而不是将它们全部声明到一个类中,这会使维护变得很困难。我想实现的目标有解决方案吗?谢谢。我这样做的原因是因为我有很多函数要作为一个方法包含,我想把它们组织成单独的文件,而不是把它们都声明成一个类,这会使维护变得很困难。我想实现的目标有解决方案吗?谢谢。如果你真的需要函数成为类的一部分,那么你必须在类中定义它们。如果你真的想管理几个文件,那么看看我提供的另一个答案:谢谢你,但是,我想这正是我所做的…我只使用了require_一次,而不是include…有什么区别吗?哦,你必须删除函数get_image{},只留下你想要执行的代码。或者helper_functions.php包含很多函数吗?