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

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();
$email->setTo('[email protected]');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
$email->send();
  $email->test1();

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?
Reply
#2

(This post was last modified: 12-18-2019, 11:54 PM by dave friend.)

What do you expect the function test1() to do?

Did you define such a function?
Reply
#3

Of course I determined! I expect her to volunteer!

Created a file \app\Libraries\Email.php:
----------------------------------------
Code:
namespace App\Libraries;
use CodeIgniter\Email\Email;

class Email extends Email
{
public function test1(){ echo("TEST!"); }
}
Reply
#4
Tongue 
(This post was last modified: 12-19-2019, 07:52 AM by dave friend.)

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.
Reply
#5

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();
Reply
#6

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;

class 
Email extends BaseEmail 
{
     //...
     Your methods


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 )
Reply
#7

Does not work!
File: \app\Libraries\Email.php:
Code:
use CodeIgniter\Email\Email as BaseEmail;
namespace App\Email;

class Email extends BaseEmail
{
    
    public function test1()
    {
        echo("TEST!");
    }
}
I call in the controller:
Code:
$email = new \App\Libraries\Email();
$email->test1();

Result:

Error
Class 'App\Email\BaseEmail' not found
APPPATH/Libraries\Email.php at line 5
Code:
<?php
use CodeIgniter\Email\Email as BaseEmail;
namespace App\Email;
class Email extends BaseEmail
{
     public function test1()
     {
         echo("TEST!");
     }
}
Reply
#8

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 )
Reply
#9

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.
Reply
#10

Hi! Earned the test1 method!

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!");
    }
}

But now sending does not work!
I call in the controller:
Code:
$email = new \App\Libraries\Email();
$email->setTo('[email protected]');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
$email->send(); // NOT WORK!
$email->test1();

If you call as follows, then sending works!
Code:
$email = \Config\Services::email(); //$email = new \App\Libraries\Email();
$email->setTo('[email protected]');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
$email->send(); // WORK!
//$email->test1();

I ask for help!
All I want to do is embed one method in the Email class, but keep the class’s functionality working!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB