Undefined property stdClass - php

why ??
A PHP Error was encountered Severity: Notice
Message: Undefined property: stdClass::$users
Filename: views/user_view.php
Line Number: 10
Backtrace:
File: C:\xampp\htdocs\ci\application\views\user_view.php Line: 10
Function: _error_handler
File: C:\xampp\htdocs\ci\application\controllers\users.php Line: 7
Function: view
File: C:\xampp\htdocs\ci\index.php Line: 315 Function: require_once

You try to access a variable without it first being defined. If you define it first, you won't get an error.
<?php
$x = new stdClass();
$y = $x->z; // gererates Notice in error_log
$x = new stdClass();
$x->z = 'assigned'; // now it is assigned
$y = $x->z; // no error
// or you could make your own class
class Whatever
{
public $z;
}
$x = new Whatever();
$y = $x->z; // it's there whether a value has been assigned or not
Check it out here https://3v4l.org/hOThV

Related

PHP: member functions can't access member variables

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".

Error passing DOMDocument through functions

Im writing a web crawler in PHP and I wrote a re-usable xPath evaluator.The function is:
function xPathEvalSingle($soruce, $xpathExpression) {
$resultsFromXpath = $source->evaluate($xPathExpression)->item(0)->textContent;
return $resultsFromXpath;
}
I create a new DOMXPath object and load from a valid HTML page:
$page = $this->getPageHtml($newCrawlUrl);
$source = new DOMXPath($page);
Then call the function:
xPathEvalSingle($soruce, $xpathExpression) = Fatal error: Call to a member function evaluate() on a non-object
However, when I do it without a function, I get what I want
$resultsFromXpath = $source->evaluate($xPathExpression)->item(0)->textContent = What I want
How would I properly pass the DOMXPath to the function to make it work?
Increase you error reporting level to include notices. You have the same variable typo $soruce in the declaration and the call of the function.
That means in the call an undefined variable is used.
Example:
<?php
error_reporting(E_ALL);
$source = 42;
print($soruce);
Output:
Notice: Undefined variable: soruce in /tmp/... on line 4
Inside the function the argument is not used, but an unknown variable $source.
Example:
<?php
error_reporting(E_ALL);
function foo($soruce) {
print($source);
}
foo(42);
Output:
Notice: Undefined variable: source in /tmp/... on line 5

"Notice (8): Undefined index: " cakephp

I am building a pdf conversion utility for my user.
I am working in CakePhp and my controller is receiving Ajax call.
Why i am getting Notice:8 error
Controller:
public function convertToPdf() {
$this->autoRender = false;
$pdf = new WkHtmlToPdf;
//$this->RequestHandler->respondAs('json');
// echo $convertData = json_encode($inputVal);
if ($this->RequestHandler->isAjax()) {
$pdfName = uniqid();
if ($_FILES['conversionSourceFile']) {
echo "File";
$pdf->addPage($_FILES['conversionSourceFile']['tmp_name']);
} elseif ($_POST['conversionSourceUrl']) {
echo "Url";
$pdf->addPage($_POST['conversionSourceUrl']);
} elseif ($_POST['conversionSourceHtml']) {
echo "Html";
$pdf->addPage('<html>' . $_POST['conversionSourceHtml'] . '</html>');
}
$saveToPath = 'upload/' . $pdfName . '.pdf';
if ($pdf->saveAs($saveToPath)) {
echo 'upload/' . $pdfName . '.pdf';
}
}
}
Error:
Notice (8): Undefined index: conversionSourceFile [APP/Controller/PdfsController.php, line 42]
Code Context
if ($this->RequestHandler->isAjax()) {
$pdfName = uniqid();
if ($_FILES['conversionSourceFile']) {
PdfsController::convertToPdf() - APP/Controller/PdfsController.php, line 42
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 109
To avoid notice in your code you have to use isset() OR !empty().
Using isset() and !empty() you can check whether variable is set and does not have an empty value.
e.g,
if (isset($_FILES['conversionSourceFile'])) {
// your code
}
OR
if (!empty($_FILES['conversionSourceFile'])) {
// your code
}
Simply check to isset $_FILES superglobal variable to prevent notice when not set.
if (isset($_FILES['conversionSourceFile'])) {
// Do more stuff
}
You need to check the first condition, with an "isset" or "!empty ()", like:
if(isset($_FILES['conversionSourceFile'])){...}
I solved the error "Notice (8): Undefined index" changing the order var $ uses.
before:
class GastosController extends AppController {
var $uses = array('Comprobante','Gasto','TipoGasto');
...
...
..
After:
class GastosController extends AppController {
var $uses = array('Gasto','Comprobante','TipoGasto');
..
..
..
Put the name that corresponds to the class (Gasto) first.

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

Symfony PHP Missing Argument Error when using Join

Hi I am using a PHP Symfony project and am trying to use the generated model peer class to make a query.
The model has generated the method "doSelectJoinMeetingItems" which is supposed to join my Meeting Actions Table to my Meeting Items table (see a quote of the generated code below).
However, when using this method I get the following error: Warning: Missing argument 1 for sfComponent::__construct(), - see in full below.
Does anyone know why this is happening? All I am doing is using Symfonys own generated methods to try and get data through my ORM to me!
Thanks for your time,
My code:
Inside MeetingMeetingsPeer
return self::doSelectJoinMeetingItems(new Criteria());
The auto generated baseMeetingActionsPeer
public static function doSelectJoinMeetingItems(Criteria $criteria, $con = null, $join_behavior = Criteria::LEFT_JOIN)
{
$criteria = clone $criteria;
// Set the correct dbName if it has not been overridden
if ($criteria->getDbName() == Propel::getDefaultDB()) {
$criteria->setDbName(self::DATABASE_NAME);
}
MeetingActionsPeer::addSelectColumns($criteria);
$startcol = (MeetingActionsPeer::NUM_COLUMNS - MeetingActionsPeer::NUM_LAZY_LOAD_COLUMNS);
MeetingItemsPeer::addSelectColumns($criteria);
$criteria->addJoin(MeetingActionsPeer::ITEM_ID, MeetingItemsPeer::ID, $join_behavior);
// symfony_behaviors behavior
foreach (sfMixer::getCallables(self::getMixerPreSelectHook(__FUNCTION__)) as $sf_hook)
{
call_user_func($sf_hook, 'BaseMeetingActionsPeer', $criteria, $con);
}
$stmt = BasePeer::doSelect($criteria, $con);
$results = array();
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$key1 = MeetingActionsPeer::getPrimaryKeyHashFromRow($row, 0);
if (null !== ($obj1 = MeetingActionsPeer::getInstanceFromPool($key1))) {
// We no longer rehydrate the object, since this can cause data loss.
// See http://propel.phpdb.org/trac/ticket/509
// $obj1->hydrate($row, 0, true); // rehydrate
} else {
$cls = MeetingActionsPeer::getOMClass(false);
$obj1 = new $cls();
$obj1->hydrate($row);
MeetingActionsPeer::addInstanceToPool($obj1, $key1);
} // if $obj1 already loaded
$key2 = MeetingItemsPeer::getPrimaryKeyHashFromRow($row, $startcol);
if ($key2 !== null) {
$obj2 = MeetingItemsPeer::getInstanceFromPool($key2);
if (!$obj2) {
$cls = MeetingItemsPeer::getOMClass(false);
$obj2 = new $cls();
$obj2->hydrate($row, $startcol);
MeetingItemsPeer::addInstanceToPool($obj2, $key2);
} // if obj2 already loaded
// Add the $obj1 (MeetingActions) to $obj2 (MeetingItems)
$obj2->addMeetingActions($obj1);
} // if joined row was not null
$results[] = $obj1;
}
$stmt->closeCursor();
return $results;
}
The Error in Full
Warning: Missing argument 1 for sfComponent::__construct(), called in /var/www/html/dev/meeting/lib/model/om/BaseMeetingActionsPeer.php on line 584 and defined in /var/www/html/dev/meeting/lib/vendor/symfony/lib/action/sfComponent.class.php on line 36
Warning: Missing argument 2 for sfComponent::__construct(), called in /var/www/html/dev/meeting/lib/model/om/BaseMeetingActionsPeer.php on line 584 and defined in /var/www/html/dev/meeting/lib/vendor/symfony/lib/action/sfComponent.class.php on line 36
Warning: Missing argument 3 for sfComponent::__construct(), called in /var/www/html/dev/meeting/lib/model/om/BaseMeetingActionsPeer.php on line 584 and defined in /var/www/html/dev/meeting/lib/vendor/symfony/lib/action/sfComponent.class.php on line 36
Notice: Undefined variable: context in /var/www/html/dev/meeting/lib/vendor/symfony/lib/action/sfComponent.class.php on line 38
Fatal error: Call to a member function getEventDispatcher() on a non-object in /var/www/html/dev/meeting/lib/vendor/symfony/lib/action/sfComponent.class.php on line 55
The lines 582 to 586 are as follows:
582 $cls = MeetingActionsPeer::getOMClass(false);
583
584 $obj1 = new $cls();
585 $obj1->hydrate($row);
586 MeetingActionsPeer::addInstanceToPool($obj1, $key1);
Line 36 from sfComponnent.class.php
36 public function __construct($context, $moduleName, $actionName)
37 {
38 $this->initialize($context, $moduleName, $actionName);
39 }
I have located the problem, in Symfony 1.4 you cannot have a class that ends in actions for example UserActions or GroupActions (wish this was properly documented somewhere).
I wish this was better documented and the error was more useful!
a classname (in the schema.yml) which ends with '%Actions' for example
UserActions? (symfony 1.4.10). An error was thrown when i added an
extra field (with validation) in the form of the object related to
this '*Actions'.
wrapped: Warning: Missing argument 2 for sfComponent::__construct(), called in
/Users/Shared/Library/symfony-1.4.10/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Table.php
on line 301 and defined in /Users/Shared/Library/symfony-1.4.10/lib/action/sfComponent.class.php on line 36
Warning: Missing argument 3 for sfComponent::__construct(), called in
/Users/Shared/Library/symfony-1.4.10/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Table.php
on line 301 and defined in /Users/Shared/Library/symfony-1.4.10/lib/action/sfComponent.class.php on line 36]
See post at http://trac.symfony-project.org/wiki/ReservedWords

Categories