Call function outside class php

I'm developing a club membership register web application and I am a fairly newbie when it comes to oop. 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.

This code will demonstrate what my problem is:

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. 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.)

asked Jul 7, 2014 at 7:11

Call function outside class php

2

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 :

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

answered Jul 7, 2014 at 7:30

MijoeMijoe

2082 silver badges9 bronze badges

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.

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);
}

answered Jul 7, 2014 at 7:27

Call function outside class php

Mobasher FasihyMobasher Fasihy

9912 gold badges8 silver badges17 bronze badges

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.

At the top of your class file, for instance, you could call another file with use of require_once. (Just to be sure you never include the same file twice, it's always good to use include_once and require_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.

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. 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.

answered Jul 7, 2014 at 7:28

ljacquljacqu

2,0821 gold badge15 silver badges21 bronze badges

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.

The test.php contains a function.

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

Maybe you need the include the file I think.

answered Jul 7, 2014 at 7:37

bixiaopengbixiaopeng

3532 silver badges11 bronze badges

In my opinion here is one solution :-

Make your file('database_functions.php') a class containing all your functions. 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.

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

answered Jul 7, 2014 at 7:50

Call function outside class php

Not the answer you're looking for? Browse other questions tagged php mysql oop or ask your own question.

How do you call a function outside the class in PHP?

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.

Can classes use functions outside the class?

Yes, definitely you can call class non-member function from class.

Can I define function outside class in Java?

Unlike C++, in Java, we cannot have just function declarations in the class and definitions outside of the class.