Question

I am a rookie PHP developer and have recently migrated from a Java developer. For my initial tutorials, I have started executing sample, demo PHP programs from the web.

The name of my website is, practicephp which is located in the root folder in my Windows machine. Hence the full path for my website is, http://localhost/practicephp.

My website contains two files, index.php (which is the default page) and Person.php which is located in a folder named, classes. As a result, the path for Person.php is, http://localhost/classes/Person.php.

The Person.php class contains a method, Hello(). I want to execute this method from the index.php page.

The source code for my files is given as follows:

index.php

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>Index</title>
    </head>
    <body>
        <?php
        // put your code here
        include './classes/';
        $p = new Person();
        $p->hello();
        ?>
    </body>
</html>

Person.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * This is the Person class
 *
 * @author Razor
 */
class Person {

    //put your code here
    public function hello() {
        echo 'Hello!';
    }

}

The problem here is that when I run the index page in my browser, the Apache web server throws me the following three errors:

  1. Warning: include(C:\xampp\htdocs\practicephp\classes): failed to open stream: Permission denied in C:\xampp\htdocs\practicephp\index.php on line 15
  2. Warning: include(): Failed opening './classes/' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\practicephp\index.php on line 15
  3. Fatal error: Class 'Person' not found in C:\xampp\htdocs\practicephp\index.php on line 16

which in other words means that Apache is neither able to locate the classes folder nor is it able to find the Person.php file. I want to import the whole classes package inside my index file. Had this been in Java, the line of code would have been, import classes.*; Can anyone please tell me its PHP equivalent?

Replies at the earliest will be highly appreciated. Thank you in advance.

Was it helpful?

Solution

Your code to include the file would have to be

    include 'classes/Person.php';
    $p = new Person();
    $p->hello();

You could use a function to load all your files like so

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}

or you could have a look at the PHP Autoload function

OTHER TIPS

In PHP you've to include every file. You are not able to include a whole directory(like in Java '*'). Ofcourse you could create a function that includes every file in a specific directory. You've also to set your security permissions on the htdocs directory(first error). If you've done above steps you'll not receive an error message anymore.

Good luck!

Instead of including a directory, include the file Person.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top