Calling one function from another file without including the whole file in php

Is it possible to call only a specific function from another file without including the whole file ??? There may be other functions in the file and there is no need to display another function.

+3
source share
3 answers

Short answer: no, you cannot.

Long answers: yes, if you use OOP.

Separate your functions into different files. Let's say you make a game with a hero:

Walk.php

function walk($distance,speed){
   //walk code
}

Die.php

function die(){
    //game over
}

Hero.php

include 'Walk.php';
include 'Die.php';

class Hero(){
    //hero that can walk & can die
}

You may have other functions, such as makeWorld()what is hero.phpnot required, so you do not need to enable it. This question has been asked several times: here and here .

, , - , .

+3

PHP .

, :

, "". .

index.php

include("class.utilities.php")
$utilities = new utilities();

class.utilities.php

class utilities {
    function __construct() {

    }
    public function thisIsTheFunction($a,$b)
    {
        $c = $a + $b;
        return $c;
    }
}

echo $utilities->thisIsTheFunction(3,4);
0

include the page, we can say that the GetPage function and the ID variable

<?php
require('page.php');

$id = ($_GET['id']);

if($id != '') {
  getpage($id);
}
?>

now when you execute the function

<?php
function getpage($id){
if ($id = ''){
//// Do something
}
else {
}
}
?>
0
source

All Articles