user6738328
user6738328

Reputation:

Namespace in php not working

I recently learn how to use namespaces on the official site. However, it is not working for me.

I have created 2 files below to test.

Example/ExampleClass.php

<?php  
namespace Example;

class ExampleClass {

    public function __construct(){
        echo 'Example Class is used by namespace';
    }
}

main.php

<?php 
use Example\ExampleClass as ExampleClass;

$example_class = new ExampleClass;

When I complied, I got

This page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500

This may be a stupid question but what is wrong with the code?

Upvotes: 3

Views: 1274

Answers (1)

MTK
MTK

Reputation: 3580

You missing include or require in your main.php script:

include_once('Example/ExampleClass.php');

use doesn't include anything. It just imports the specified namespace (or class) to the current scope if they are before included

Upvotes: 3

Related Questions