Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an Greeting.php with

<?php
    namespace sayinghello;
    class Greeting {
        public function getName() {
            return "Hello!";
        }
    }

and the calls into the sayinghello.php:

<?php
    $greeting = new Greeting();
    echo $greeting->getName();

When I tried to open the sayinghello.php with my Browser, the Class-Not-Found-Exception would be thrown

('Fatal error: Uncaught Error: Class 'sayinghelloGreeting' not found in C:UsersUserIdeaProjectsEfPsrcsayinghellosayinghello.php:2 Stack trace: #0 {main} thrown in C:UsersUserIdeaProjectsEfPsrcsayinghellosayinghello.php on line 2

I use PHP with the XAMPP-Stack and the solutions of similar questions does not solved the problem...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
195 views
Welcome To Ask or Share your Answers For Others

1 Answer

It now works. I needed to add two statements:

include 'Greeting.php';

AND

use sayinghelloGreeting;

Im programming with IntelliJ and only be familiar with Java. Thaugt that PHP will look automaticaly in the namespace for the classes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...