Question

here is my folders and files:

Folder Main
 folder a
   - fila a.php
 folder b
   - file b.xml
 folder c
   - file c.php
index.php

when I try to do this its shows error any suggestion or fixes.

file c.php

<?php
class Abc{
public function __construct(){
    $xml = simplexml_load_file('b/b.xml');
}}?>

so I include file c.php in a.php, which going to show error

<?php require '../c/c.php'; $abc = new Abc(); ?>

file index.php

<?php
   require 'c/c.php';
   $abc = new Abc();
 ?>`

but if I include in index.php no prob. How do I fix the path ('b/b.xml') in c.php so that I can include it anywhere in the project.


I found the way to fix it. by doing this

file c.php

$xml = simplexml_load_file(dirname(dirname(__FILE__)).'/b/b.xml');

file a.php

include dirname(dirname(__FILE__)).'/c/c.php';
$abc = new Abc();

in file index.php

require dirname(__FILE__).'/c/c.php';
$abc = new Abc();
Was it helpful?

Solution

This is the simple idea use in Joomla / Wordpress / Cakephp & other open source.

In your index.php, use:

require( dirname( __ FILE__ ).'/c/c.php');

and in class construction, use:

$xml = simplexml_load_file(dirname( __ FILE__ ) .'/b/b.xml');

Let me know if this works.

Thanks, Munjal

OTHER TIPS

You can modify the c.php as follows:

<?php

     class Abc
        {
            public function __construct()
               {
                 $xml = simplexml_load_file('mainfoldername/b/b.xml');
               }
        }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top