How to extend class Email? |
Hello colleagues!
Created a file \app\Libraries\Email.php: ---------------------------------------- namespace App\Libraries; use CodeIgniter\Email\Email; class Email extends Email { public function __construct() { parent::__construct(); } public function test1() { echo("TEST!"); } } ------ I call in the controller: Code: $email = \Config\Services::email(); Gives an error message: Error Call to undefined method CodeIgniter\Email\Email::test1() ll to undefined method CodeIgniter\Email\Email::test1() APPPATH/Controllers\Home.php at line 38 Code: 38 $email->test1(); How to add test1 method to Email class?
What do you expect the function test1() to do?
Did you define such a function?
Of course I determined! I expect her to volunteer!
Created a file \app\Libraries\Email.php: ---------------------------------------- Code: namespace App\Libraries;
My mistake. It was late and I clearly did not read your code well enough.
What you have done should work. Maybe, if possible, update to the current dev commit. See if that helps.
I could be wrong here, but your library email class is namespaced App\Libraries\Email, but you call \Config\Services::email() in your code. Those are two different things. I believe you should be using
Code: $email = new \App\Libraries\Email\Email();
The way you are doing it should have gave a re-declare class error.
IF you want to use the same name as the core CodeIgniter class then you should do it like this. PHP Code: use CodeIgniter\Email\Email as BaseEmail; Notice the as in the use cause. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Does not work!
File: \app\Libraries\Email.php: Code: use CodeIgniter\Email\Email as BaseEmail; Code: $email = new \App\Libraries\Email(); Result: Error Class 'App\Email\BaseEmail' not found APPPATH/Libraries\Email.php at line 5 Code: <?php
That's because the namespace needs to be on the first line
PHP Code: <?php namespace your\namespace; What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
You've also namespaced the class App\Email, but then you're calling new \App\Libraries\Email(); The namespaces don't match so the class won't be found an you'll have another error after you've fixed what InsiteFX pointed out.
Hi! Earned the test1 method!
File: \app\Libraries\Email.php: Code: <?php But now sending does not work! I call in the controller: Code: $email = new \App\Libraries\Email(); If you call as follows, then sending works! Code: $email = \Config\Services::email(); //$email = new \App\Libraries\Email(); I ask for help! All I want to do is embed one method in the Email class, but keep the class’s functionality working! |
Welcome Guest, Not a member yet? Register Sign In |