0

I'm working with two files - a.php which contains the class Background, and b.php which includes a.php . *In a.php , out of the class scope , there's a echo statement " background check" .

When I load b.php , I can see the output "background check" , but when I try to create a background object the next warning message appears :

Fatal error: Class 'Background' not found in ....

Here's a code sample from b.php :

<?php
        include ('http://localhost/wT/sf/a.php');
        $url2="http://www.google.com";
        $b = new Background($url2);
?>
3
  • It is not possible includen a url Commented Jul 24, 2012 at 13:37
  • It is if the URL returns only source code. It's bad practice, but it can be done. Commented Jul 24, 2012 at 13:37
  • Does include from urls even work? Commented Jul 24, 2012 at 13:50

5 Answers 5

4

If the output of http://localhost/wT/sf/a.php (in a web browser) isn't PHP source code, then your include will not work. You probably (in your case) have to include the file from the filesystem, not through an HTTP address.

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

3 Comments

The include function returns the next error : failed to open stream: No such file or directory
Might want to make sure the document root is listed in include_path. Then, just include stuff relative to it.
Or just use the file's absolute path to include it.
3

Use relative routes for the includes, not a URL.

In other words, the file that you are including has to be on the same server (or able to be accessed on the same server) as the file that you are including it in.

2 Comments

The include works though , becuase I can see the output of the echo statement from a.php ...the problem is that I can not create the object I've created in a.php
Loading php-executable content by url may work, but it is wrong by concept
1

Try something like this to make this work:

<?php
        include ('/var/www/wT/sf/a.php'); //your filesystem location to a.php
        $url2="http://www.google.com";
        $b = new Background($url2);
?>

3 Comments

Include using the filesystem location does not work : here's the file location : wT/sf/a.php
please post content of a.php to better help you
the file location can't be wT/sf/a.php, there should be something liek /var/www/.. what is your server's root directory?
0

In include method you should use relative file path

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file

Comments

0

You are using web-url instead of use system path like

<?php
        include ($_SERVER['document_root'].'/a.php');
        $url2="http://www.google.com";
        $b = new Background($url2);
?>

1 Comment

better include dirname(__FILE__).'/a.php';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.