Suhail Gupta
Suhail Gupta

Reputation: 23276

why doesn't it find the class when it finds the script?

There is some weird thing going on.

Following is a snippet from view_reminders.php :

<?php 
  namespace reminder;
  require_once('./reusable/reminders.php');
?>

some HTML...

<?php  
    $reminders = new Reminders(); # STATEMENT THAT THROWS AN ERROR : LINE 23
    $reminder_details = $reminders->get_reminders();
    foreach($reminder_details as $reminder) {
        echo $reminder;
    }
?>

In the above script , the statement $reminders = new Reminders() throws an error : Fatal error: Class 'reminder\Reminders' not found in E:\Installed_Apps\xampp\htdocs\remind\view_reminders.php on line 23.

I do not understand this error. Following is the snippet from reminders.php :

<?php
namespace reminder;
namespace connection;
require_once('./reusable/connection.php');

Class Reminders{
    private $user_info;
    private $userID;
    private $reminder_info;
    private $reminder_count;

            .
            .
            .
    }

Now why doesn't it find the class Reminders when it finds the script reminders.php ?

Note: The directory structure :

     +--->resuable
          +---->connection.php
           ---->reminders.php
     ---->view_reminders.php

view_reminders.php is just outside connection.php and reminders.php that are inside the reusable directory.

Upvotes: 1

Views: 75

Answers (4)

Sepster
Sepster

Reputation: 4848

According to PHP: Defining multiple namespaces in the same file :

Multiple namespaces may also be declared in the same file.

In "reminders.php", you've declared multiple namespaces as per the syntax described on that page. I'd suggest the latter one (connection) is the namespace under which your Reminders class has been created.

Upvotes: 0

Anton
Anton

Reputation: 1051

Can you really use two namespaces that way??.. as i know, in this code:

namespace reminder;
namespace connection;

next code will go for connection namespace, and reminder is empty

try to remove connection namespace from there.

Upvotes: 0

jeroen
jeroen

Reputation: 91792

You are setting your namespace to connection. You can use multiple namespaces, but you would have to write it like:

<?php
namespace connection;
require_once('./reusable/connection.php');

namespace reminder;
Class Reminders{
    private $user_info;
    private $userID;
    private $reminder_info;
    private $reminder_count;

            .
            .
            .
    }

Also check the link to the manual for the recommended way to use multiple namespaces using brackets:

<?php
namespace connection {
require_once('./reusable/connection.php');
}

namespace reminder {
Class Reminders{
    private $user_info;
    private $userID;
    private $reminder_info;
    private $reminder_count;

            .
            .
            .
    }
}

Upvotes: 2

ulentini
ulentini

Reputation: 2412

Double namespace on Reminders class?

Upvotes: 0

Related Questions