PHP: member functions can't access member variables - php

I'm experimenting with PHP classes and I've created this simple class:
<?php
/* email.php
Returns a wrapper object that handles the sending of an email
*/
class Email
{
private $message = "";
private $subject = "";
private $to = "";
public function setMessage(string $newMessage) {
$this->$message = $newMessage;
}
public function setSubject(string $newSubject) {
$this->$subject = $newSubject;
}
public function setRecipient(string $newRecipient) {
$this->$to = $newRecipient;
}
public function send() {
mail($this->$to, $this->$subject, $this->$message);
}
}
$email = new Email();
$email->setRecipient('test#gmail.com');
$email->setSubject('A message');
$email->setMessage('Does this work?');
$email->send();
?>
However, when I run this (I am calling this script via a fetch request on my fronted), I get the following error:
Notice: Undefined variable: to in /home/jack/Projects/test/php/email.php on line 24
Notice: Undefined variable: subject in /home/jack/Projects/test/php/email.php on line 20
Notice: Undefined variable: message in /home/jack/Projects/test/php/email.php on line 16
Notice: Undefined variable: to in /home/jack/Projects/test/php/email.php on line 28
Notice: Undefined variable: subject in /home/jack/Projects/test/php/email.php on line 28
Notice: Undefined variable: message in /home/jack/Projects/test/php/email.php on line 28
What am I doing wrong here?

That's because you're doing $this->$subject, you mean $this->subject.
Fuller answer:
PHP allows you to have custom variable names. E.g. you could set
$i = 0;
$var$i = "abc";
Then $var0 would contain "abc".

Related

Message: Undefined variable: get

I'm trying to join my table and it's working but I don't know why I got the following error messages.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get
Filename: views/tambah_wali.php
Line Number: 13
Fatal error: Call to a member function result() on a non-object in C:\xampp\htdocs\ujikom_sekolah\application\views\tambah_wali.php on line 13
This is my controller:
public function tambah_wali($kode){
$data['get']=$this->madm->join_wali_siswa($kode);
$this->load->view('tambah_wali');
}
This is my model:
public function join_wali_siswa($kode) {
$this->db->select('wali_murid.nisn');
$this->db->from('siswa');
$this->db->join('wali_murid','siswa.nisn=wali_murid.nisn','right');
$this->db->where('siswa.nisn',$kode);
$query=$ambildata=$this->db->get();
if($ambildata->num_rows > 0)
return $query;
else
return null;
}
This is line 13 on my view:
<?php foreach($get->result() as $row): ?>
Load your $data['get'] in your view seems to be missing when have a $data variable must add it to view like below.
$data['get'] = $this->madm->join_wali_siswa($kode);
$this->load->view('tambah_wali', $data);

returning a json encode on an api layer i'm getting the following errors

api layer (move_shopping_list.php):
if (!isset($_GET['i_sign_in_token'])){
die(json_encode('Must pass in the i_sign_in_token into the url'));
}
if (!isset($_GET['itm_cd'])){
die(json_encode('Must pass in itm_cd into the URL'));
}
if (!isset($_GET['list_id_old']) || !isset($_GET['list_id_new'])){
die(json_encode('Must pass in the list_id_old or new value into the URL'));
}
$list = new ShoppingListItem($_GET['i_sign_in_token'], $_GET['itm_cd'], $_GET['list_id_old'], $_GET['list_id_new']);
$moveItems = $item->move_between_lists($token, $_GET['itm_cd'], $_GET['list_id_old'], $_GET['list_id_new']);
echo json_encode($moveItems);
Here is my move between lists method inside of my ShoppingListItem class:
public function move_between_lists($shopper, $new_list_id) {
global $pd, $db;
// todo: don't forget to update $this->ShoppingList
$vars = array();
$vars[] = array('i_sign_in_token', $shopper);
$vars[] = array('itm_cd', $this->ITM_CD);
$vars[] = array('list_id_old', $this->SHOPPING_LIST_ID);
$vars[] = array('list_id_new', $new_list_id);
$rows = $db->get_function_as_proc('custom.japi_shopping_list.Move_Bewteen_Lists(:i_sign_in_token, :itm_cd, :list_id_old, :list_id_new)', $vars);
if ($rows == 'Y') {
// Must have worked or it would have returned.
$this->SHOPPING_LIST_ID = $new_list_id;
return true;
} else {
return false;
}
}
I keep getting these errors and i have no idea why.. any help would be greatly appreciated.
Notice: Undefined index: i_sign_in_token in /var/www/api/move_shopping_list.php on line 3 Notice: Undefined index: itm_cd in /var/www/api/move_shopping_list.php on line 7 Notice: Undefined index: list_id_old in /var/www/api/move_shopping_list.php on line 11 Notice: Undefined index: list_id_new in /var/www/api/move_shopping_list.php on line 15 Notice: Undefined index: i_sign_in_token in /var/www/api/move_shopping_list.php on line 19 Notice: Undefined index: itm_cd in /var/www/api/move_shopping_list.php on line 19 Notice: Undefined index: list_id_old in /var/www/api/move_shopping_list.php on line 19 Notice: Undefined index: list_id_new in /var/www/api/move_shopping_list.php on line 19 Notice: Undefined variable: item in /var/www/api/move_shopping_list.php on line 20 Fatal error: Call to a member function move_between_lists() on a non-object in /var/www/api/move_shopping_list.php on line 20
If the array key in $_GET is not set, you are explicitly checking whether or not this nonexistant key is also not an empty string. The logic is wrong, you must not use && (and) but || (or).

Undefined variable in __construct()... but I did define it

I've written the following piece of code:
Class stackOverflowExample {
private $hash;
private $cookie_file;
public function __construct(){
#session_start();
if(isset($_SESSION['gc_hash'])){
$this->$hash = $_SESSION['gc_hash'];
}else{
$this->$hash = md5(time());
$_SESSION['gc_hash'] = $this->$hash;
}
$this->$cookie_file = "./cookies/{$this->$hash}.txt";
}
}
But I'm getting this error
Notice: Undefined variable: hash in
/var/www/gausie/gc/GeoCaching.Class.php on line 21
Fatal error: Cannot access empty property in
/var/www/gausie/gc/GeoCaching.Class.php on line 21
In the original code, line 21 refers to $this->$hash = $_SESSION['gc_hash'];.
I can't see why this is happening, although I'm new to OO PHP. Any ideas?
just replace $this->$hash by $this->hash
$this->$hash means variable with name equals to variable $hash value

Notice: Undefined variable: this in C:\dev\workspaces\adxweb\library\Adx\Datagrid\class.eyedatagrid.inc.php on line 885

Hii,
When I am trying to use eyedatagrid, I am getting the following error message and the datagrid is not displayed, any idea why I am getting this error message, thanx
Notice: Undefined variable: this in
Datagrid\class.eyedatagrid.inc.php on
line 885
and line 885 holds this ----
if ($this)
{
$page = $this->page;
$order = (($this->order) ? implode(':', $this->order) : '');
$filter = (($this->filter) ? implode(':', $this->filter) : '');
}
You are calling the function in static context (as I just deduced from the code you didn't show).
EyeDataGrid::printJavascript()
This is why $this isn't present.
Change the if ($this) into if (isset($this)) or disable the debug mode if you don't want notices.

Error defining variable in PHP object

I am getting the following error in the following code:
Class primeField implements field {
private $intmodulus = '';
public function generator(){
return ;
}
public function modulus(){
return $this->$intmodulus;
}
public function __construct($modulus , $base=0) {
if (is_resource($modulus) && get_resource_type($modulus) == "GMP integer"){
$this->$intmodulus = $modulus;
} else{
$this->$intmodulus = gmp_init($modulus , $base); \\line 70
}
}
}
$a = new primeField(11);
$a->modulus();
Notice: Undefined variable: intmodulus in /Users/admin/PHP ECC/finitefield.php on line 70
Fatal error: Cannot access empty property in /Users/admin/PHP ECC/finitefield.php on line 70
Why
The syntax is
$this->intmodulus
not $this->$intmodulus.
You get an error saying "cannot access empty property" because $intmodulus is undefined and hence accessing it gives NULL. The NULL gets converted into an empty string when you attempt to use it as a property name.
If the value of $intmodulus was the name of a valid property (e.g. if $intmodulus == "intmodulus"), you would be accessing the property with that name.
$this->$intmodulus should be $this->intmodulus
See the PHP documentation for Variable variables for info on what is happening.

Categories