I searched, and found this question, which helped me:
php static variable is not getting set
It did, however, not solve my entire problem.
Code:
Class DummyClass {
public static $result;
function __construct() {
$this->_setResultCode('testing');
}
public function getResultCode() {
return self::$result['code'];
}
private function _setResultCode($val) {
echo 'Im gonna set it to: ' . $val . '<br />';
self::$result['code'] = $val;
echo 'I just set it to: ' . $this->getResultCode;
die();
}
}
Outputs:
Im gonna set it to: testing
I just set it to:
What's going on here? How is this even possible?
EDIT: The problem was i missed the parentheses when calling getResultCode(). HOWEVER, i have another issue now. I can't seem to get the resultCode out of the class (later on in another instance of DummyClass).
Here is my relevant coded (No more example code because i seemed to mess that up):
Class lightweightContactFormPlugin {
// Set up/Init static $result variable
public static $result;
function __construct() {
//echo 'I just inited<br/><pre>';
//var_dump($this->getResultCode());
//echo '</pre><br/>';
}
public function run() {
// Set default value for resultCode
$this->_setResultCode('no_identifier');
// Check if form was posted
if(isset($_POST['cfidentifier'])) {
$fields = $this->_get_fields_to_send();
$valid = $this->_validate_fields($fields);
// Only continue if validatation was successful
if($valid == true) {
// Store mail result in $mail
$mail = $this->_send_mail($fields);
// Yay, success!
if($mail) {
$this->_setResultCode('sent_successfully');
return;
} else {
// Couldn't send mail, bu-hu!
$this->_setResultCode('not_sent');
return;
}
}
$this->_setResultCode('validation_fail');
return;
}
}
// Get and Set methods
public function getResultCode() {
return isset(self::$result['code']) ? self::$result['code'] : '';
}
private function _setResultCode($val) {
self::$result['code'] = $val;
}
}
Left some irrelevant methods out. None of the other methods set or get the resultCode, it shouldn't matter.
Any ideas why i can't access $result['code'] in another instance of the object (further down the page)?
I do this when i access it:
$plugin = new lightweightContactFormPlugin();
$cfstat = $plugin->getResultCode();
echo '<pre>';
var_dump($fstat);
echo '</pre>';
Result is:
NULL
The strange thing is, if i uncomment the code in __construct(), the CORRECT value does get printed out! But if i try to access it from getResultCode() after, it returns NULL again. What is going on?
echo 'I just set it to: ' . $this->getResultCode;
I think you're missing a few parenthesis here.
You have no return within getResultCode()
You have to call getResultCode() as a method.
echo 'I just set it to: ' . $this->getResultCode();
A. your code is wrong ... there is nothing like __constructor in PHP it should be
function __construct() {
B. Your code should also return the following since getResultCode was not set
Notice: Undefined property: DummyClass::$getResultCode
You should be calling
echo 'I just set it to: ' . $this->getResultCode();
Your Final Code :
class DummyClass {
public static $result;
function __construct() {
$this->_setResultCode('testing');
}
public function getResultCode() {
return self::$result['code'];
}
private function _setResultCode($val) {
echo 'Im gonna set it to: ' . $val . '<br />';
self::$result['code'] = $val;
echo 'I just set it to: ' . $this->getResultCode();
die();
}
}
new DummyClass();
Output
Im gonna set it to: testing
I just set it to: testing
use return like this
public function getResultCode() {
return self::$result['code'];
}
and use $this->getResultCode();
EDIT
the only problem i can see is you have just written return; , because of that it return NULL, change it to
return $this->getResultCode();
Related
I cannot workout why this script always returns 0. If I change it to echo getSKU() it works, but Quantity, Price or Name never seems to work. If anybody has any ideas please, please help this is irritating the life out of me!
<?php
session_start();
$sku = "0001";
if (!isset($_SESSION[$sku])) {
$_SESSION[$sku] = new product($sku, 5);
} else {
}
echo $_SESSION[$sku]->getQuantity();
class product {
var $sku;
var $name;
var $price;
var $quantity;
function __construct($par1, $par2) {
$this->sku = $par1;
$this->quantity = $par2;
}
function setSKU($x) {
$this->sku = $x;
}
function getSKU() {
echo $this->sku;
}
function setName($x) {
$this->name = $x;
}
function getName() {
echo $this->name;
}
function setPrice($x) {
$this->price = $x;
}
function getPrice() {
echo $this->price;
}
function setQuantity($x) {
$this->quantity = $x;
}
function incrementQuantity() {
$this->quantity++;
}
function getQuantity() {
echo $this->quantity;
}
}
You should use return instead of echo. Your get...-methods currently don't return something (just implicitly null), they just echo the value you want to return.
To fix this, just replace in every get...-method echo with return - i.e.
function getQuantity() {
return $this->quantity;
}
In addition to that, you should know, that you cant store objects in $_SESSION (actually you could, but then you have to implement the magic __sleep and __wakeup-methods..).
You should think about other solutions to store your products inside the session (i.e. serialize them)
you shouldn't echo your attribute in get methodes
echo $this->Variable;
you should always return them.
return $this->Variable;
return returns program control to the calling module. Execution
resumes at the expression following the called module's invocation
for more information on return check the documentation here
while the issues brought up in the other answers should definitely be addressed, to answer your question i believe the quantity is probably not set. can you try adding this line?
$_SESSION[$sku]->setQuantity(5);
$_SESSION[$sku]->getQuantity();
Working on a project of translating website and I had chose this solution
.
I'm trying to accomplish something like :
$VAR1 = $translate->__('Word_To_Translate');
This, not works for me since, the result is directly shown in stdout of the webpage. Even so when trying to call $VAR1 no result is returned.
This is not easily possible with the class you've mentioned.
If you wish to edit the class so it'll return the value instead of echoing it, you can edit class.translation.php, replace the two occurances of echo $str; with return $str;, and replace echo $this->lang[$this->language][$str]; with return $this->lang[$this->language][$str] (simply changing echo to return on both instances).
//$VAR1 delegating
$VAR1 = $translate->__('Word_To_Translate');
//class.translation.php
`class Translator {
private $language = 'en';
private $lang = array();
public function __construct($language){
$this->language = $language;
}
private function findString($str) {
if (array_key_exists($str, $this->lang[$this->language])) {
return $this->lang[$this->language][$str];
return;
}
return $str;
}
private function splitStrings($str) {
return explode('=',trim($str));
}
public function __($str) {
if (!array_key_exists($this->language, $this->lang)) {
if (file_exists($this->language.'.txt')) {
$strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
foreach ($strings as $k => $v) {
$this->lang[$this->language][$v[0]] = $v[1];
}
return $this->findString($str);
}
else {
return $str;
}
}
else {
return $this->findString($str);
}
}
}`
Switched the echo for a return
Thank you very much uri2x && Rizier123.
For the moment looks that it is working solution.
Best wishes !
How can i pass a class as a parameter in my function
So far i've tried
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
this is a simplified example of what im doing.. i actually have to do complex procedures inside this sample function. I'm not getting any response from the sample function. What am i doing wrong? thank you
UPDATE
char.php
class Charss {
var $name=0;
var $hp=500;
var $spd=10;
var $rtime=10;
var $dmg=10;
function __construct( $name, $hp, $spd, $rtime , $dmg) {
$this->name = $name;
$this->hp = $hp;
$this->spd = $spd;
$this->rtime = $rtime;
$this->dmg = $dmg;
}
function get_name() {
return $this->name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_hp() {
return $this->hp;
}
function set_hp($new_hp) {
$this->hp = $new_hp;
}
function get_spd() {
return $this->spd;
}
function set_spd($new_spd) {
$this->spd = $new_spd;
}
function get_rtime() {
return $this->rtime;
}
function set_rtime($new_rtime) {
$this->rtime = $new_rtime;
}
function get_dmg() {
return $this->get_dmg;
}
function set_dmg($new_dmg) {
$this->dmg = $new_dmg;
}
}
myclass.php
require("char.php");
class Person {
function try_process()
{
$chr1 = new Charss("Player1",500,3,0,50);
$chr2 = new Charss("Player2",500,6,0,70);
while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
{
$sth = min($chr1->get_rtime(), $chr2->get_rtime());
if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
{
exit;
Fight($chr1,$chr2);
$chr1->set_rtime($chr1->get_spd());
}
elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
{
Fight($chr2,$chr1);
$chr2->set_rtime($chr2->get_spd());
}
else
{
Fight($chr1,$chr2); #having trouble with this
$chr1->set_rtime($chr1->get_spd());
}
$chr1->set_rtime($chr1->get_rtime() - $sth);
$chr2->set_rtime($chr2->get_rtime() - $sth);
}
}
function Fight($atk,$def)
{
$def->set_hp($def->get_hp() - $atk->get_dmg());
echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}
}
so im calling the function try_process on button click
What you're actually doing there is passing an object, not a class.
$sc = new SampleClass();
creates an instance of SampleClass, aka an object.
I assume there's some error being thrown elsewhere as what you have is correct.
I tested the following code and got the expected output:
class SampleClass
{
public function getValue()
{
return 4;
}
}
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
Output: 4
If you provide more details of your actual code we might be able to determine the problem.
I can't see anything wrong with your code
using &$refClass is however is not recommended and I guess willbe removed from future iteration of PHP version
but here is an example
class objects are passed as reference I suppose so no need of '&'
http://ideone.com/GbmUy
Why is the function argument a reference? Probably shouldn't be.
Other than that, there's nothing wrong with you posted, so the error is likely within SampleClass.
Others have answered pretty well, but this is a silly little example to show you how to modify the class (either by calling a property setter, or setting public properties directly)
class foo {
private $member1;
public $member2;
public function __construct($member1,$member2) {
$this->member1=$member1;
$this->member2=$member2;
}
public function SetMember1($value) {
$this->member1 = $value;
}
public function GetMember1() {
return $this->member1;
}
}
function SetMembers(foo $obj, $member1, $member2) {
// Call a setter
$obj->SetMember1($member1);
// Set a member variable directly
$obj->member2 = $member2;
}
$obj = new foo('default member 1', 'default member 2');
echo "member1 (before): {$obj->GetMember1()}\n";
echo "member2 (before): {$obj->member2}\n";
// Change values
SetMembers($obj, 'new member1', 'new member2');
echo "member1 (after): {$obj->GetMember1()}\n";
echo "member2 (after): {$obj->member2}\n";
This will output:
member1 (before): default member 1
member2 (before): default member 2
member1 (after): new member1
member2 (after): new member2
I'm very new to PHP and OOP in general. I'm using codeigniter for a framework, and am currently attempting to build a class 'BuildLinks' that will redirect the user to the correct link based on what URL they landed on.
The controller passes the right variables to the class, while the function build_afflink() selects what class to call next based on the var1
The controller:
function out($var1, $var2)
{
// redirect to link after parsing data
$debug='1';
$params = array('var1'=>$var1, 'var2'=>$var2);
$this->load->library('BuildLinks', $params);
if ($debug=='0'){
$redirect = $this->buildlinks->build_afflink();
redirect($redirect, 'location', 301);
}
else {
var_dump($this->buildlinks->build_afflink());
}
}
The class BuildLinks is a work in progress... but it is extended by all of the other sites I need to support.
BuildLinks class:
class BuildLinks
{
public $var1;
public $var2;
public $link;
function __construct($params)
{
//populate up inititial variables from $params array (passed from controller)
$this->var1 = (string)$params['var1'];
$this->var2 = (string)$params['var2'];
echo __class__ . ' loaded....' . 'var1: '.$this->var1 . ' var2: ' .$this->var2. '<br/>';
}
public function get_var1()
{
return $this->var1;
}
public function set_var1($var1)
{
$this->var1 = $var1;
}
public function get_var2()
{
return $this->var2;
}
public function set_var2($var2)
{
$this->var2 = $var2;
}
function build_thelink()
{
switch ($this->var1) {
case 'amazon':
//echo 'Amazon is our vendor.<br>';
$newobj = new Amazon;
// Amazon subclass returns the correct affiliate link
return $newobj->affiliate_link();
break;
case 'ebay':
$newobj = new Ebay;
//ebay subclass however, cannot access the public var var1, it returns a null value for $this->var1
return $newobj->affiliate_link();
break;
}
}
}
Essentially, when I make a new Ebay object, it can't access any of the public variables from the parent class BuildLinks. What am I doing wrong?
In this example, I have it construct the initial variables and echo back out some information for debugging.
EDIT: If I change the __construct to read:
function __construct($params)
{
//populate up inititial variables from $params array (passed from controller)
$this->var1 = (string)$params['var1'];
$this->var2 = (string)$params['var2'];
echo __class__ . ' loaded....' . 'var1: '.$this->var1. ' var2: ' .$this->var2 . '<br/>';
var_dump(get_class_vars(get_class($this)));
}
Then I get this as an output:
BuildLinks loaded....var1: ebay var2: somedata
array
'var1' => null
'var2' => null
'link' => null
The following works fine:
$newobj = new Amazon;
return $newobj->affiliate_link();
This does not, but the classes are almost identical...
$newobj = new Ebay;
return $newobj->affiliate_link();
Here is the ebay class:
class Ebay extends BuildLinks
{
private $res;
//TODO: language/multiple site support
//public $locale;
function __construct()
{
//echo __class__ . ' loaded....' . 'vendor: '.$this->vendor . ' isbn: ' .$this->isbn . '<br/>';
}
function add_to_cart_button($isbn, $locale)
{
}
function affiliate_link()
{
$this->get_info();
return $this->link;
}
// Load $this->api_call() and return Amazon SimpleXML response object, load variables
function get_info()
{
$apicall = $this->api_call();
//build variables
foreach ($apicall->searchResult->item as $item) {
$this->link = (string )$item->viewItemURL;
}
}
// Generate API call and return simplexml object $res
function api_call()
{
//build $apicall here
$res = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($res->ack == "Success") {
$this->res = $res;
return $res;
} else {
echo 'api_call() unsuccessful';
}
}
}
This part of the ebay class doesn't make too much sense to me:
function api_call()
{
//build $apicall here
$res = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($res->ack == "Success") {
$this->res = $res;
return $res;
} else {
echo 'api_call() unsuccessful';
}
}
simplexml_load_file($apicall); specifically isn't loading anything. Where's $apicall? If you debug that, does it echo "api_call() unsuccessful"?
Because if that's unsuccessful then Ebay::link won't be set and as a result return $newobj->affiliate_link(); wouldn't end up returning anything.
for clarification, $apicall should be set to a file path before calling simplexml_load_file($apicall);
Ebay::get_info() does this
foreach ($apicall->searchResult->item as $item) {
$this->link = (string )$item->viewItemURL;
}
If $apicall->searchResult->item happens to have several elements, last of which is empty, you'll get only that last empty element.
That's a wild guess though.
[added]
You're overwriting a constructor in Ebay, so if you want BuildLinks' class constructor to be called, you need to do this explicitly.
I'm looking for something like break for loops.
Here's some example code (using Symfony's lime) where stop() would not let the class continue and I_DONT_WANT_THIS_TO_RUN() would not be executed.
$browser->isStatusCode(200)
->isRequestParameter('module', 'home')
->isRequestParameter('action', 'index')
->click('Register')
->stop()
->I_DONT_WANT_THIS_TO_RUN();
$browser->thenThisRunsOkay();
Calling $this->__deconstruct(); from within stop() doesn't seem to do the trick. Is there a function I can call within stop() that would make that happen?
You could use PHP exceptions:
// This function would of course be declared in the class
function stop() {
throw new Exception('Stopped.');
}
try {
$browser->isStatusCode(200)
->isRequestParameter('module', 'home')
->isRequestParameter('action', 'index')
->click('Register')
->stop()
->I_DONT_WANT_THIS_TO_RUN();
} catch (Exception $e) {
// when stop() throws the exception, control will go on from here.
}
$browser->thenThisRunsOkay();
Just return another class which will return $this for every method called.
Example:
class NoMethods {
public function __call($name, $args)
{
echo __METHOD__ . " called $name with " . count($args) . " arguments.\n";
return $this;
}
}
class Browser {
public function runThis()
{
echo __METHOD__ . "\n";
return $this;
}
public function stop()
{
echo __METHOD__ . "\n";
return new NoMethods();
}
public function dontRunThis()
{
echo __METHOD__ . "\n";
return $this;
}
}
$browser = new Browser();
echo "with stop\n";
$browser->runThis()->stop()->dontRunThis()->dunno('hey');
echo "without stop\n";
$browser->runThis()->dontRunThis();
echo "the end\n";
Will result in:
with stop
Browser::runThis
Browser::stop
NoMethods::__call called dontRunThis with 0 arguments.
NoMethods::__call called dunno with 1 arguments.
without stop
Browser::runThis
Browser::dontRunThis
the end
OIS's answer is really good, though I could see that it might get confusing if the object suddenly changes to something else. That is, you'd expect that at the end of your chain, you'll end up with the same object. To avoid that problem, I'd add a private variable to tell the class whether or not to actually do anything. If the class has been stopped, then every class just returns $this straight away. This gives you the added benefit of being able to restart the execution.
class MyClass {
private $halt;
function __call($func, $args) {
if ($this->halt) {
return $this;
} else {
return $this->$func($args);
}
}
private function isRequestParameter() {
// ...
}
public function stop() {
$this->halt = true;
}
public function start() {
$this->halt = false;
}
}
This could be put into a parent class so you don't have to duplicate this code.