PHP - Why am I getting this error? Call to undefined function - php

I am new to php, so sorry if this is lame.
I have a simple class that has two private functions. For some reason when I try to call _sendInvalidEmailNotice() I get an error stating that the function is undefined.
What am I doing wrong?
class Mail extends CI_Controller {
function __construct() {
parent::__construct();
$this - > load - > helper('email');
}
function _sendMessage($message) {
send_email('name#gmail.com', 'Test Email', $message);
$success = array('success' = > 'Mail Sent');
echo json_encode($success);
}
function _sendInvalidEmailNotice() {
$errorMessage = array('error' = > 'Invalid Email Address');
echo json_encode($errorMessage);
}
public
function sendMail($returnAddress, $message) {
if (valid_email($returnAddress)) {
_sendMessage($message);
} else {
_sendInvalidEmailNotice();
}
}
}

You need to add context.
$this->_sendInvalidEmailNotice();

Related

How to implement Custom channel notifications in Laravel

I'm trying to write my custom laravel channel notifications, like this: https://laravel.com/docs/5.7/notifications#custom-channels.
I wrote this code:
AimonChannel.php
class AimonChannel{
public function send($notifiable, Notification $notification)
{
$message = $notification->toAimon($notifiable);
if (is_string($message)) {
$message = new AimonMessage($message);
}
//some code to send sms with curl
}
AimonMessage.php
class AimonMessage
{
public $messaggio = "";
public function messaggio($messaggio){
$this->messaggio = $messaggio;
}
}
SendAimonMessage.php
class SendAimonMessage extends Notification
{
use Queueable;
protected $messaggio;
public function __construct($messaggio)
{
$this->messaggio = $messaggio;
}
public function via($notifiable)
{
return [AimonChannel::class];
}
public function toAimon($notifiable)
{
return (new AimonMessage())
->messaggio($this->messaggio);
}
}
So, the code:
$user->notify(new SendAimonMessage('my custom message'));
is sent, but without the text.
The problem is in the send() function of AimonChannel; the $message variable is always null.
Where is my code mistake?
Thanks!
add return statement in the messaggio function like this:
class AimonMessage {
public $messaggio = "";
public function messaggio($messaggio){
$this->messaggio = $messagio;
return $this;
}
}

Error when calling global variables in codeigniter

I want to declare a global variable with a value and access it in the functions.
I'm done below code but showing Undefined variable: msg1 in the login function.
Please help me to do this.
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->$msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Try this,
class loader extends CI_Controller {
public $msg1 = "login error";
public function __construct() {
parent::__construct();
}
public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->msg1;
$this->index($msg) ;
} else {
//something here
}
}
}
}
Remove $ from $msg1 in login function
$msg = $this->msg1;

Accessing variable inside a static function class

I have a login.php and authenticate.php
I want to access a variable inside the authenticate.php inside a class.
I want to get the error message from authenticate class to my login.php
This is my class inside Authenticate.php
Class Authenticate {
public $invalidUserErrMsg = "sdfs";
static public function LDAPAuthenticate() {
//connection stuff
} else {
$msg = "Invalid username / password";
$this->invalidUserErrMsg = $msg;
}
}
static public function invalidUserErr() {
echo $hits->invalidUserErrMsg;
return $this->invalidUserErrMsg;
}
}
This is how I'm printing inside login.php
<?php
$error = new Authenticate();
$error->invalidUserErr();
?>
Class Authenticate {
public $invalidUserErrMsg = "sdfs";
public function LDAPAuthenticate() {
if($hello) {
echo 'hello';
} else {
$msg = "Invalid username / password";
$this->invalidUserErrMsg = $msg;
}
}
public function invalidUserErr() {
return $this->invalidUserErrMsg;
}
}
<?php
$error = new Authenticate();
echo $error->invalidUserErr();
?>
Don't echo the variable within the class but echo the method on login.php. There is no need to make it a static function if you are going to instantiate the object anyway.
Check out this page on the static keyword
For accessing static function you need
<?php
$error = new Authenticate::invalidUserErr();
?>

how to stop all class function if one class function return false

I am creating a mail class. I want to stop all function if one function return false,
call the class
<?php
$mail= new mail();
$mail->from('abc#gmail.com');
$mail->to('jhon#gmail.com');
$mailsend=$mail->send();
if($mailsend){echo 'mail send';}else{echo 'not send';}
?>
the class
<?php
class mail{
public function from($email){
/*
* email validation
* if not valid return false and stop then send and other functions
*/
}
public function to(){
}
public function send(){
}
}
?>
Consider using Exceptions.
Wrap your code in try..catch block and throw an Exception when a problem is found.
Like this:
class mail {
public function from($addr) {
if(not valid addr) throw new Exception("Invalid from address");
}
...
}
try
{
$mail = new mail();
$mail->from("invalid email address");
$mail->to("to#example.com");
$mail->send();
}
catch(Exception $e)
{
echo "Email not sent: " . $e->getMessage()."\n";
}
This way, once from() has discovered an invalid address, it will throw an exception, and the execution will stop at that point and resume inside catch block
combine all into a send function
class mail{
public function send($to,$from){
//do the mailing
if(!emailIsValid())
return false;
//and so on
//if all is fine ..validation etc
return true;
}
}
then call it like so
<?php
$mail= new mail();
if($mail->send('abc#gmail.com','jhon#gmail.com')){echo 'mail send';}else{echo 'not send';}
?>
One way to address this is to chain your method calls. To do this, each method in your class should return the instance itself except for the last one send(). Every method prior to that is merely for setting data and then the send() method will trigger a validation.
So your call would look something like...
$mail = new Mail();
$mail->to('abc#gmail.com')
->from('xyz#gmail.com')
->send();
And your class could look like:
class Mail {
protected $to = null;
protected $from = null;
public function to($email)
{
// You should filter here
$this->to = $email;
return $this;
}
public function from($email)
{
// You should filter here
$this->from = $email;
return $this;
}
public function send()
{
// Validate
if ($this->validate())
{
// Sending logic
}
else
{
// Return or show error
}
}
public function validate()
{
// Verify email values are valid
}
}
Because it is all neatly packaged as a single call chain, you can return an error up the chain or throw an exception as others have suggested.
make a (private) variable valid in your class:
class mail{
private $valid;
public function from($email){
//validation
$valid = false||true; //the return value
}
public function to(){
}
public function send(){
if ($valid) {
//send mail
return true
} else {
return false;
}
}
}
It isn't the best way to do it but this way is most similar to your version

Check call_user_func_array return data PHP

How to check call_user_func_array return data PHP:
i have class 1:
class MyClass
{
public function show()
{
echo 'This is show function';
}
}
class 2:
class MyClass2
{
public function show2()
{
return 'This is show2 function';
}
}
now, i check:
if(isset(call_user_func_array(array(new MyClass,'show'),$params)))
{
echo 'Has return data';
}
else
{
call_user_func_array(array(new MyClass,'show'),$params);//run normal function
}
if(isset(call_user_func_array(array(new MyClass2,'show2'),$params)))
{
echo 'Has return data';
}
else
{
call_user_func_array(array(new MyClass2,'show2'),$params);//run normal function
}
but not working, somebody can help me?
Warning
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
http://www.php.net/manual/en/function.isset.php

Categories