We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Abstract class not working

Hi Guys,

I have an abstract class which I use to display messages. i have a piece of code surrounded by a try / catch block but it seems the message is never passed to the Exception. When I put a hard coded message in the exception it works.

Can I anybody help me with this. Heres my code

// The Abstract Class

namespace App\Models;

asbstract class Messages
{
    const ERROR_MSG = "This is just an error message";
    const LOGIN_DENIED = "Login has been denied";
}

In my controller action I have this :

namespace App\Controllers;

use App\Messages;

class AuthController extends BaseController
{

    public function loginAction()
    {
        try{
            $loggedIn = false;
            if (!$loggedIn) {
                throw new \Exception(Messages::LOGIN_DENIED);
            }
        } catch(\Exception $e) {
            // The Message is never displayed
            echo $e->getMessage();
        }
    }
}

Please help. Thanks

Your messages class is using the namespace: App\Models, and you use: App\Messages (after setting an App\Controllers namespace)

Does throw new \Exception(\App\Models\Messages::LOGIN_DENIED); work?



22.8k

No, the namespaces are correct. I reference the Messages class by using the "use" keyword.



22.8k
Accepted
answer
edited Feb '15

I found the error, there was a duplicate const word in the class that just broke everything, the class has about 100 messages inside. Whats funny though Netbeans didnt pickup the issue and Phalcon did not issue a warning, error or notice.