0

Suppose I have following files

<?php 
     include 'file_A';
     include 'file_B';
 ?>

a function defined foo() in file_B, is there any way to use it in file_A by any means???

actually, I am working in a application where a range of different functions are defined in modules, I want to use these function across the modules without looking where it defined included before or after the file even.

3
  • I would say no, but following your question, maybe I hope not. Commented Apr 18, 2011 at 3:38
  • 1
    Spider-code, spider-code, does whatever, some spider-code does. Can it call, a random function, no it can't, because it's interpreted. ... Try having some structure to your modules. When you generate a call graph and it looks like a hideous spiderweb, you're doing something wrong. Commented Apr 18, 2011 at 3:42
  • @Borealid - Yeah, but we have all been there, out of control of what code was running, yet responsible. Commented Apr 18, 2011 at 3:44

1 Answer 1

2

As PHP is an interpreted language, you cannot do this.

What I would do as a solution is extract the methods you need to share into a CommonMethods class or something similar. Make them static if possible.

Then include the CommonMethods file before file_A and file_B.

I'd spend a little time researching design patterns for PHP before continuing your project. Spaghetti code is never a good thing.

Sign up to request clarification or add additional context in comments.

3 Comments

Spaghetti code? Did you say spaghetti code? I like spaghetti. I like ravioli. Can you program in ravioli? Gnocchi? I like Italian food.
+1 for mentioning Spaghetti code... I haven't heard that term for a very long time
It's the first thing I think of when PHP is mentioned. Not necessarily due to the language itself, but the programmers I've dealt with who utilize it. I've seen terrible examples of spaghetti code. And not just regular spaghetti, we're talking angel hair.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.