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
Related
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".
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
Ive been trying to get this to work all day but it doesnt seem to work. The $login variable was called but it doesnt affect the if statement:
$login = new Login();
function inclu(){
if ($login->isUserLoggedIn() == true){
include (($_SERVER['DOCUMENT_ROOT']."/project/log/assets/displayuser.php"));
}
else {
// the user is not logged in. you can do whatever you want here.
// for demonstration purposes, we simply show the "you are not logged in" view.
include (($_SERVER['DOCUMENT_ROOT']."/project/log/assets/displaysign.php"));
};
}
It comes up with the errors:
Notice: Undefined variable: login in C:\xampp\htdocs\project\log\index.php on line 38
Fatal error: Call to a member function isUserLoggedIn() on a non-object in C:\xampp\htdocs\project\log\index.php on line
You need to declare $login again in the function:
function inclu() { global $login; /*...*/ }
the first line in the inclu function must be 'global $login;' to get the global variable login accessible inside the function. it's a special scoping technique of PHP
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
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.