0

I have projectA which has a.php and I have projectB which has b.php How do i access a.php from b.php.All this projects lie in the same root as HOME Please help



    <?php
    class B {
        function getB() {
            return "B";
        }
    }   
<?php
    include '/projectB/B.php';
     class AA {
        function getA() {
            $objB=new B();
            echo $objB->getB();
        }
    }

    $obj=new AA();
    $obj->getA();
    ?>

Error thrown is Class 'B' not found in C:\wamp\www\projectA\AA.php

a YUY
  • 87
  • 9

4 Answers4

0

if a.php and b.php is in the same folder:

in b.php:

<?php
  include_once(__DIR__ . '../projectA/A.php');
?>

__DIR__ is the dynamic path of folder from b.php.

http://php.net/manual/en/language.constants.predefined.php

if it is very important that a is include in b.php use require:

<?php
  require_once(__DIR__ . '../projectA/a.php');
?>
goldlife
  • 1,949
  • 3
  • 29
  • 48
  • And provide the complete path if it is in another directory – Hanky Panky Jan 14 '15 at 07:41
  • There is absolutely no need for `__DIR__` as include is always relative to the directory of the file in which it is being called from. – SimonDowdles Jan 14 '15 at 07:46
  • It is wrong to say that it is important that the OP use 'require' or 'require_once', it all depends on his scenario. – SimonDowdles Jan 14 '15 at 07:50
  • in some cases it is better to use __DIR__ with an include. It does not hurt to write these few letters. and as I said, IF it is important that one file include to another he has to use require, in some other cases it is enough to use include. – goldlife Jan 14 '15 at 07:52
  • The two files are not in the same folder,they are in different projects.However the two projects are in the same folder.And the ans is not working. – a YUY Jan 14 '15 at 08:01
0

just requre a.php from b.php here is the code:

require_once('a.php');

Kibo
  • 870
  • 1
  • 15
  • 30
0

If file a.php is in another directory, the path can be either relative (to your current file a.php directory) or absolute (based on your servers absolute file path).

Example of relative:

<?php 
     include('../a.php'); // one directory back, as the file path is always relative to the file in which include (or require or any of the others) is being called.
?>

Example of absolute:

<?php
     include('/path/to/server/root/some/folder/a.php'); // absolute path 
?>

Hope this helps.


UPDATE

You keep saying "it's wrong" or "it doesn't work" but you really have not explained your file structure very well. So I am going to ASSUME that your folder structure looks like this, for examples sake:

  • WEB ROOT
    • ProjectA
      • a.php
    • ProjectB
      • b.php

Now, if you were inside of a.php, you could require b.php as follows:

require_once('../ProjectB/b.php');

If you were inside of b.php and wanted to include a.php, it could be as follows:

include_once('../ProjectA/a.php');

Note, I have used include AND require, it's up to you to research which to use.

SimonDowdles
  • 2,026
  • 4
  • 24
  • 36
  • It should also be said that there are other methods, such as require, require_once, include and include_once. To get a good idea of the differences, read this post on StackOverflow: http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include – SimonDowdles Jan 14 '15 at 07:47
  • It is throwing an error saying Warning: include(): Failed opening '/projectB/B.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\projectA\AA.php on line 2 – a YUY Jan 14 '15 at 08:03
  • You are using an absolute path in your comment. / means that you are starting FROM the system root, eg "C:" on windows or "/" on linux / unix. Try either "projectB/B.php" or "./projectB/B.php" – SimonDowdles Jan 14 '15 at 08:08
  • Thanks it works when i give the full path include 'C:\wamp\www\projectB\B.php'; Is there any better ways i can handle this?I mean if i deploy the code tomorrow in another system,the path changes – a YUY Jan 14 '15 at 08:28
  • Yes, using `__DIR__` will result in the full absolute path to the directory that your file is currently in. I don't understand why you want to use absolute paths when relative paths are hassle free and won't break. Also, bear in mind that directory separators on windows ("\") are different to Linux, Unix & OSX which all use "/". You can replace your slashes with the PHP constant "DS" – SimonDowdles Jan 14 '15 at 08:43
  • Thanks,I would go with relative paths,as they are hassle free.Can i also address my problem using namespaces?What are my options if i donot want to write includes in my files? – a YUY Jan 14 '15 at 09:00
  • You have no choice but to include or require the file if they are separate files. Even if you name spaced, you'd still need a reference to class A (or B) from the other file. If I were you, I would ignore name spacing for now. When you have a better understanding of OOP, take a look then. – SimonDowdles Jan 14 '15 at 09:45
0

Classes are confined to the 'space' in which they are created. You can not, in theory, cross pollinate classes.

You have two options:

1) Create a Singleton class (see Global PHP class in functions?)

2) Extend class AA from class BB, example:

Class AA extends class B{
   // Yada yada yada
}

It's worth reading the following two resource:

http://php.net/manual/en/keyword.extends.php

http://php.net/manual/en/language.oop5.inheritance.php

Community
  • 1
  • 1
SimonDowdles
  • 2,026
  • 4
  • 24
  • 36