Welcome Guest, Not a member yet? Register   Sign In
How to extend class Email?
#11

To use one of your own system classes instead of a default one, ensure that the Autoloader can find your class, that your new class extends the appropriate interface, and modify the appropriate Service to load your class in place of the core class.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#12

(12-23-2019, 04:22 AM)InsiteFX Wrote: To use one of your own system classes instead of a default one, ensure that the Autoloader can find your class, that your new class extends the appropriate interface, and modify the appropriate Service to load your class in place of the core class.
Please show by example how to do this?
Reply
#13

Look at the Config/Services.php email method that will show you how to do it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#14

(This post was last modified: 12-23-2019, 10:21 PM by videoproc.)

Thanks! Everything works!

In the file \ app \ Config \ Services.php added:
Code:
class Services extends CoreServices
{

    public static function email($config = null, bool $getShared = true)
    {
        if ($getShared)
        {
            return static::getSharedInstance('email', $config);
        }
        if (empty($config))
        {
            $config = new \Config\Email();
        }
        $email = new \App\Libraries\Email($config); // $email = new \CodeIgniter\Email\Email($config);
        $email->setLogger(static::logger(true));
        return $email;
    }
}

File: \app\Libraries\Email.php:
Code:
<?php
namespace App\Libraries;
use CodeIgniter\Email\Email as BaseEmail;

class Email extends BaseEmail
{
    public function test1()
    {
        echo("TEST!");
    }
}
Call in the controller:
Code:
$email = \Config\Services::email();
$email->setTo('[email protected]');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
$email->send(); // WORK!
$email->test1(); // WORK!
Reply
#15

A super trivial bit of code that does not affect the outcome but is maybe an interesting alternate syntax example.

PHP Code:
<?php
namespace App\Libraries;

use 
CodeIgniter\Email\Email as BaseEmail;

class 
Email extends BaseEmail
{
    .... 
The code above could also be written

PHP Code:
<?php
namespace App\Libraries;

class 
Email extends \CodeIgniter\Email\Email 
{
    ... 

So the second example drops the "use" clause and defines the parent class with a fully qualified namespace instead.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB