Unable to Load Class under a NameSpace

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
phpnewbie2019
New php-forum User
New php-forum User
Posts: 7
Joined: Sat May 18, 2019 4:42 am

Sat May 18, 2019 7:20 pm

I have a test PHP project. The project file structure is below.

Code: Select all

NetBeansProjects/php-basics/
├── autoLoader.php
├── cEntities
│   └── cEmployees.php
├── Chapter7
│   └── advancedOOPFeatures.php
├── iEntities
│   └── iEmployees.php
├── index.php
├── meta
│   └── nbproject
│       ├── private
│       │   └── private.properties
│       ├── project.properties
│       └── project.xml
└── tEntities

7 directories, 8 files
I have created a separate file for the autoloader that php pages will need to include.
file autoloader.php

Code: Select all

spl_autoload_register(function ($objectName) {
    switch ($objectName[0]) {
        case 'c':
            include_once './cEntities/' . $objectName . '.php';
            break;

        case 'i':
            include_once './iEntities/' . $objectName . '.php';
            break;

        case 't':
            include_once './tEntities/' . $objectName . '.php';
            break;

        default:
            break;
    }
});
Index.php is the entry point where the autoloader is included for the entire project.

Code: Select all

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        include_once 'autoLoader.php';
        ?>
        
        <a href="Chapter7/advancedOOPFeatures.php">Chapter 7 Examples</a><br />
    </body>
</html>
I have an Interface named iEmployees.php

Code: Select all

<?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.
 */

namespace php_basics\iEntities;

/**
 *
 * @author sheenlim08
 */
interface iEmployees {
    function getFirstName();
    function getLastName();
    function getFullName();
    function getAge();
    
    function setFirstName($fName);
    function setLastName($lName);
    function setAge($age);
}
and a Class cEmployees that implements iEmployees

Code: Select all

<?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.
 */

namespace php_basics\cEntities;

/**
 * Description of cEmployees
 *
 * @author sheenlim08
 */
class cEmployees implements \php_basics\iEntities\iEmployees {
    private $firstName;
    private $lastName;
    private $age;
    
    function __construct($fName, $lName, $age) {
        $this->firstName = $fName;
        $this->lastName = $lName;
        $this->age = $age;
    }
    
    function setFirstName($fName) {
        $this->firstName = $fName;
    }
    
    function setLastName($lName) {
        $this->lastName = $lName;
    }
    
    function setAge($age) {
        $this->age = $age;
    }
    
    function getFirstName() {
        return $this->firstName;
    }
    
    function getLastName() {
        return $this->lastName;
    }
    
    function getAge() {
        return $this->age;
    }
    
    function getFullName() {
        return $this->getFirstName() . " " . $this->getLastName();
    }
}
When I navigate to advancedOOPFeatures.php

Code: Select all

<html>
    <head>
        <meta charset="UTF-8">
        <title>Chapter 7</title>
    </head>
    <body>
        <?php
        include_once '/home/sheenlim08/NetBeansProjects/php-basics/autoLoader.php';
        
        use php_basics\cEntities;
        
        $emp1 = new cEntities\cEmployees("Sheen Ismhael", "Lim", 29);
        
        printf("Employee 1: {$emp1->getFullName()}, is age: {$emp1->getAge()}");
        ?>
    </body>
</html>
The line where is says

Code: Select all

$emp1 = new cEntities\cEmployees("Sheen Ismhael", "Lim", 29);
generates an error shown below.
[Sun May 19 11:05:59 2019] PHP Fatal error: Uncaught Error: Class 'php_basics\cEntities\cEmployees' not found in /home/sheenlim08/NetBeansProjects/php-basics/Chapter7/advancedOOPFeatures.php:19
Stack trace:
#0 {main}
thrown in /home/sheenlim08/NetBeansProjects/php-basics/Chapter7/advancedOOPFeatures.php on line 19
[Sun May 19 11:05:59 2019] 127.0.0.1:55332 [500]: /Chapter7/advancedOOPFeatures.php - Uncaught Error: Class 'php_basics\cEntities\cEmployees' not found in /home/sheenlim08/NetBeansProjects/php-basics/Chapter7/advancedOOPFeatures.php:19
Stack trace:
#0 {main}
thrown in /home/sheenlim08/NetBeansProjects/php-basics/Chapter7/advancedOOPFeatures.php on line 19

Why can't it see the cEmployees class? when I told advancedOOPFeatures to use the "php_basics\cEntities" namespace? am I missing something?
Last edited by phpnewbie2019 on Sat May 18, 2019 8:10 pm, edited 1 time in total.
phpnewbie2019
New php-forum User
New php-forum User
Posts: 7
Joined: Sat May 18, 2019 4:42 am

Sat May 18, 2019 8:09 pm

Looks like this a include_once issue after all.
In the advancedOOPFeatures.php

if I add the following code just before the "use" statement, i can run the page just fine.

Code: Select all

include_once './../iEntities/iEmployees.php';
include_once './../cEntities/cEmployees.php';
so that means that it is not the namespace that i'm having problem with, its the autoloader I created "audoloader.php".

Is it possible to include autoloader that is declared from another file used in the same way as my code in the first post?
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 854
Joined: Mon Feb 22, 2016 5:52 pm

Sun May 19, 2019 7:40 am

As per the other post: read the link for autoloading; PHP is happy to help with namespaces in the autoloader.
Post Reply