-1

I have 2 projects that'll like to share functions. Function-A, in project-A, should be called by function-B, in project-B. Any quickest way to achieve it?

Example function-A

function upload($file, $path){
   $CFG->file    = $file;
   $CFG->path    = $path;
}

How do I call this function in function-B, project-B

function callUpload(  ){
   upload( "a.png", "application/path/file" );
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
ken4ward
  • 2,246
  • 5
  • 49
  • 89
  • What is Project-B? Where are the files located? Do you know PHP has classes? – Alberto Aug 29 '18 at 15:42
  • Project-B, is the authentication app, while project-A is the main app for products. One runs on XAMPP, while the other on MAMP. They are both PHP projects. – ken4ward Aug 29 '18 at 15:44
  • @Barmar, boss, why down-vote the question? Just want to know sir. Thanks. – ken4ward Aug 29 '18 at 15:52
  • Anyone can downvote, and it is unlikely somehow who took the time to edit your question also downvoted. At any rate, this question seems like something better suited for a tutorial site because it is not quite a specific coding question; this may be the reason for the downvotes. What research have you done? I'm sure there are plenty of tutorial and info sites that talk about how to share PHP functions. –  Aug 29 '18 at 16:25
  • @kehinde I didn't downvote, I just edited the question to fix the misspelling in the title. – Barmar Aug 29 '18 at 17:30
  • @Barmar, thanks boss. Appreciate. – ken4ward Aug 30 '18 at 07:12

1 Answers1

-1

Create a file that you include() at the top of each one. This file would contain the function, and then can be used across script:

include('functions.php');

Or create a Class and use that in the same way.

functions.php

function upload($file, $path){
   $CFG->file    = $file;
   $CFG->path    = $path;
}

scriptA.php & scriptB.php

include('functions.php');
upload($file, $path);

For further reference, check this out: How to call function of one php file from another php file and pass parameters to it?

Adam
  • 1,294
  • 11
  • 24
  • Thanks for the quick response. i meant across 2 PHP projects. This is for directories within the same projects. – ken4ward Aug 29 '18 at 15:43
  • 1
    You could use a full qualified path for it if you are able to provide the full path, as ana example,`include('/var/projects/shared/functions.php')` – Adam Aug 29 '18 at 15:46
  • No problem, my answer on here will give you more information about relative and absolute paths: https://stackoverflow.com/questions/52003582/accessing-a-stylesheet-from-a-php-file/52003658#52003658 – Adam Aug 29 '18 at 15:49
  • Thanks sir. highly honored for this kindness. – ken4ward Aug 29 '18 at 15:50
  • Happy to help! If it works for you and my answer helped you, don't forget to mark it as correct =] – Adam Aug 29 '18 at 15:52
  • 5 years on but did this work for you? – Adam Jun 28 '23 at 20:18