简体   繁体   English

PHP-类之外的类调用函数

[英]PHP - Class calling functions outside the class

I'm developing a club membership register web application and I am a fairly newbie when it comes to oop. 我正在开发俱乐部会员注册Web应用程序,当涉及到时,我还是一个新手。 The problem I'm having is that I would need to call a function outside a class, but I know you can't do that in PHP and I need to solve it somehow. 我遇到的问题是,我需要在类外调用函数,但是我知道您无法在PHP中做到这一点,我需要以某种方式解决它。

This code will demonstrate what my problem is: 此代码将演示我的问题是:

members.php members.php

class member {
    private $id;
    private $member_num;
    private $name;
    ...
    ...

    public function __construct() { 
        $a = func_get_args();
        $i = func_num_args();
        if (method_exists($this,$f='__construct'.$i)) {
            call_user_func_array(array($this,$f),$a);
        } 
    }

    private function __construct1($f_id) { // Construct object by id-number
        // Code to retrieve the data from database
    }

I've written the code for database actions in a separate file called database_functions.php, but I can't include that file inside a class. 我已经在名为database_functions.php的单独文件中编写了数据库操作的代码,但是我无法将该文件包含在类中。 What I need to know is how can I access those functions without having to write them again inside the class? 我需要知道的是如何访问这些函数而不必在类中再次编写它们? (I have written applications before with VB.Net and in it I can use functions of the current project inside classes.) (我之前使用VB.Net编写过应用程序,并且可以在其中使用类中当前项目的功能。)

you can use require_once() with exact path of that class after that you should use the object of the included class and function name for calling the function of outer class. 您可以将require_once()与该类的确切路径一起使用,然后再使用包含的类的对象和函数名称来调用外部类的函数。

if(file_exists($class_path))
{
   require_once($class_path);
   $cl_obj = new name_of_class();
   $outer_class_result = call_user_func_array(array($cl_obj,$function_name),$parametrs);
}

If you have a main file that calls different files (via include or require ), then you can use any function that will exist in global namespace just fine in that class, provided the function you call will always exist in global namespace. 如果您有一个调用不同文件(通过includerequire )的主文件,那么您可以使用将在全局命名空间中存在的任何函数,只要在该类中就可以,只要您调用的函数始终存在于全局命名空间中即可。

At the top of your class file, for instance, you could call another file with use of require_once . 例如,在类文件的顶部,您可以使用require_once调用另一个文件。 (Just to be sure you never include the same file twice, it's always good to use include_once and require_once .) (只是确保您不会两次包含同一文件,使用include_oncerequire_once总是很好的。)

The other options is to wrap the other functions you have inside another class, provided that it makes sense. 其他选择是将您拥有的其他函数包装在另一个类中,只要它有意义。 Class A could then hold a field with a class B object and call B 's functions as it pleases. 然后,类A可以保存带有类B对象的字段,并根据需要调用B的函数。

class classA {
    private $BInstance;

    function __construct($b) {
        $this->BInstance = $b;
    }

    function some_call($input) {
        if($this->valid_input($input)) {
            $this->BInstance->transform($input);
        }
    }
}

The benefit of this is that everything is abstracted in classes. 这样做的好处是,所有内容都抽象在类中。 Once you're outside of the class, all you have to know is what methods (functions) you can use of a class and you won't have to worry about the implementation details. 一旦离开了类,您只需要知道可以使用类的哪些方法(函数),就不必担心实现细节。 A direct advantage of this is that you don't have to pass the same parameter to same functions all the time, say, if you need a database handler to execute SQL code within a function. 这样做的直接好处是,您不必一直将相同的参数传递给相同的函数,例如,如果您需要数据库处理程序来执行函数中的SQL代码。 A class could just hold such an instance and you won't have to repeat the same parameter all the time. 一个类可以只保存一个这样的实例,而您不必一直重复相同的参数。

An additional advantage is that your global namespace won't be polluted with specific functions. 另一个优点是您的全局名称空间不会受到特定功能的污染。

You can extend a class to another class to access parent class functions and properties in subclass. 您可以将一个类扩展到另一个类,以访问子类中的父类函数和属性。 Functions declared private cannot be accessed like that. 声明为私有的函数不能那样访问。 You can also pass object of another class to to one class as an argument to the constructor. 您还可以将另一类的对象作为构造函数的参数传递给一个类。

if you have a class name parent : 如果您的班级名称为parent:

class parent{
    // properties and methods
}

Then you can access its functions if you extend it to your class : 然后,如果将其扩展到您的类,则可以访问其功能:

class child extends parent{
    // code
} 

OR 要么

$parent = new parent();
$child = new child($parent);

Then in child class you can use : 然后在子类中可以使用:

function __construct($parent) {
        $this->connection = $parent;
}

Then within other functions in child class you can access the parent class methods using 然后,在子类中的其他函数中,您可以使用以下方法访问父类方法

$parent->connection->function_name()

I'm not sure if you include the file which defined the functions you want. 我不确定您是否包含定义所需功能的文件。 I tried this with my symfony preject and it works well. 我用我的symfony投影仪尝试过,效果很好。

The test.php contains a function. test.php包含一个函数。

<?php 
function double_number($value)
{
    return $value*2;
}
?>

and I include it in the Model: 我将其包括在模型中:

include(__DIR__.'/test.php');
class HomeModel
{
...
    public function __construct()
    {
        $this->preExecute();
        echo double_number(5);
        exit;
    }
...
}

and it output whith 10. 并输出10。

Maybe you need the include the file I think. 也许您需要包含我认为的文件。

In my opinion here is one solution :- 我认为这是一种解决方案:

Make your file('database_functions.php') a class containing all your functions. 使您的文件('database_functions.php')成为包含所有函数的类。 Like.. 喜欢..

class mydbcls{
  public function function_1(){}
  public function function_2(){}
  public function function_3(){}
  //......  
  public function function_n(){}
}

Now when you are writing your class which is named as "member", write it in such a way that it extends your mydbcls. 现在,当您编写名为“成员”的类时,以扩展mydbcls的方式编写它。

class member extends mydbcls{
    private $id;
    private $member_num;
    private $name;
    ...
    ...

    public function __construct() { 
        $a = func_get_args();
        $i = func_num_args();
        if (method_exists($this,$f='__construct'.$i)) {
            call_user_func_array(array($this,$f),$a);
        } 
    }

    private function __construct1($f_id) { // Construct object by id-number
        // Code to retrieve the data from database
        // Now you can access your database functions in this way
        // $this->function_1();
    }

now you can access your db functions this way 现在您可以通过这种方式访问​​数据库功能

$this->function_1();

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

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