简体   繁体   English

php 我包含了一个文件但无法调用 class 中的函数

[英]php i have included a file but cant call functions which are inside class

i have included a file in another file and now i want to use on of the functions from the included file.我已将一个文件包含在另一个文件中,现在我想使用包含文件中的功能。

when i go to the included file and put a variable there the variable get passed and its all good当我 go 到包含的文件并在那里放置一个变量时,变量被传递并且一切都很好
- that means i have includedd the right file. - 这意味着我包含了正确的文件。

but when i want to call a funcion which is inside a class但是当我想调用 class 中的函数时

class SimpleViewer {
.
.
.
.
function whatever(){}

it just doesnt call it when i each it and writes当我每个它并写入时它只是不调用它

Fatal error: Call to undefined function display_gallery_table()...致命错误:调用未定义的 function display_gallery_table()...

i know for sure im in the right file because the other values passed but i just can call nothing because its insidfe the class我确定我在正确的文件中,因为其他值已通过,但我什么也不能调用,因为它包含 class

what am i doing wrong?我究竟做错了什么?

Try initiating an instance of the class first like this尝试像这样首先启动 class 的实例

$myClass = new SimpleViewer();

then call the function like this然后像这样拨打 function

$myClass->whatever();
$var = new SimpleViewer();
$var->whatever();

You need to create an instance of the object first;您需要先创建一个 object 的实例;

$obj = new Class();
$obj->func();

There are many ways to call a function between classes.有很多方法可以在课间调用 function。 The first I know is a static method call.我知道的第一个是 static 方法调用。 You can call function by defining class::function() .您可以通过定义class::function()来调用 function。

example:例子:

public class Parent{
    public static function check(){
        ....
        ....
    }
}

the static caller may look alike static 来电者可能看起来很像

Parent::check()

Second way is initiate the class as the new object. From the example we can do the call like第二种方法是将 class 启动为新的 object。从示例中我们可以像这样调用

$obj = new Parent;
$obj->check();

the rest manual of implementing this is on php manual rest 实施手册在php 手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM