I've the following snippet:
public function __construct($s, $e, &$v, &$rv, $needle, $parser, $id) {
$this->start = $s;
$this->end = $e;
$this->vett = $v;
$this->resVett = &$rv;
$this->id = $id;
$this->needle = $needle;
$this->parser = $parser;
}
public function pushResult($result) {
$this->resVett[] = $result;
}
When I call pushResult the value is properly inserted but the original array, the one referenced by rv, is not affected.
Have you any idea of what could be the problem?
maybe the problem relies elsewhere. I checked your code with an easy example
<?php
class Example {
public $value = [];
public $reference = [];
public function __construct($value, &$reference) {
$this->value = $value;
$this->reference =& $reference;
}
public function pushResult($result) {
$this->reference[] = $result;
$this->value[] = $result;
}
}
$value = ['test'];
$reference = ['test'];
$example = new Example($value, $reference);
$example->pushResult('result');
echo 'Is Value variable same?';
var_dump($value === $example->value);
echo 'Is Reference variable same?';
var_dump($reference === $example->reference);
and passing the reference like you do it should work
How pass parameter to PHP class by class()::function()?
class greenHouse{
public function __construct(connection $con){
}
public function show(){
}
}
$nameclass = 'greenHouse';
$namefunction = 'show';
$nameclass::$namefunction();
works
$nameclass = 'greenHouse';
$namefunction = 'show';
$nameclass($con)::$namefunction();
doesn't work
I want to pass a parameter to the class with $nameclass($con)::$namefunction();. How do I do that in PHP?
You are trying to call a function statically with that notation...
$nameclass = 'greenHouse';
$namefunction = 'show';
$class = new $nameclass($con);
$class->$namefunction();
You can instantiate an object and immediately discard it by calling new within braces:
class Test
{
private $name;
function __construct($name)
{
$this->name = $name;
}
function speak()
{
echo $this->name;
}
function __destruct()
{
echo 'dead';
}
}
$class='Test';
$method='speak';
(new $class('David'))->$method();
echo ' is ';
$temp = new $class('John');
$temp->$method();
echo ' is ';
//Daviddead is John is dead
So in your case:
(new $nameclass($con))->$namefunction();
Just of my curiousity I have a class that has
class someCLass {
var $_var1 = '';
var $_var2 = '';
public function _set(){}
public function _get(){}
public function _put(){}
}
Is it possible to call this function dynamically. For example:
public function insomefunc(){
$key_sample = 'set';
$result = $this->_$keysample(); //call dynamically a function which should be _set()
}
the same way for a variable
public function insomefunc(){
$var_sample = 'var1';
$this->_$varsample = 'jackpot' //assign
}
Want to know answers for enlightment. Thank you
You will have to add the "_" in your string:
public function insomefunc(){
$key_sample = 'set';
$result = $this->{'_'.$keysample}(); //call dynamically a function which should be _set()
}
See http://php.net/manual/en/functions.variable-functions.php
You can do like this..
<?php
class someCLass
{
var $_var1 = '';
var $_var2 = '';
public function set()
{
echo "I am set";
}
public function get()
{
}
public function put()
{
}
public function runset()
{
$key_sample = 'set';
$this->$key_sample();
}
}
$a = new someCLass();
$a->runset();
OUTPUT :
I am set
I have a function w/in a function, and I need the inner function to make it's variables available in a scope of parent function, e.g.:
function sayMyName(){
getName(); // inner function generates $name value
echo $name; // use $name
}
sayMyName();
I could easily just globalize things w/in both functions... But my situation is far more complicated and handles more variables and globalizing each one is a bit tedious.
Thanks.
PS
i noticed a lot of "return" suggestions. sorry i wasnt clear , i need to return more variables.. not a simple return. thanks guys
You may use $_GLOBALS, but it`s a "bad practice". So,
1: Use return:
<?php
function getName(){
$name = 'Smith';
return $name;
}
function sayMyName(){
$name = getName();
echo $name;
}
sayMyName();
?>
Shows:
Smith
2: Use references:
<?php
function getName(&$name){
$name = 'Smith';
}
function sayMyName(){
getName($name);
echo $name;
}
sayMyName();
?>
Shows:
Smith
3: Return array for multiple variables:
<?php
function getName(){
$surname = 'Smith';
$name = 'John';
return array($surname, $name);
}
function sayMyName(){
list($surname, $name) = getName();
echo $name, ' ', $surname;
}
sayMyName();
?>
Shows:
John Smith
4. Return custom object for multiple variables:
<?php
function getName(){
$surname = 'Smith';
$name = 'John';
$buffer = new stdClass();
$buffer->name = $name;
$buffer->surname = $surname;
return $buffer;
}
function sayMyName(){
$obj = getName();
echo $obj->name, ' ', $obj->surname;
}
sayMyName();
?>
Shows:
John Smith
5. Use anonymous function with use statement and references:
<?php
function sayMyName(){
$surname = $name = 'Unknown';
$temp = function() use (&$name, &$surname){
$surname = 'Smith';
$name = 'John';
};
$temp();
echo $name, ' ', $surname;
}
sayMyName();
?>
Shows:
John Smith
do this
function sayMyName(){
$name = getName(); // inner function generates $name value
echo $name; // results will be returned
}
sayMyName();
I hope your inner function is returning name like this
function getName(){
return $name;
}
then it will work
This is what the object oriented programming was designed for. If many functions should share variables, it is probably best to encapsulate them to class like this:
class WhateverDescibestYourViewOfTheWorld {
protected $name;
function __construct( $name)
{
$this->name = $name;
}
function GetName() {
return $this->name;
}
function SayName()
{
echo $this->name;
}
}
// And use it:
$o = new WhateverDescibestYourViewOfTheWorld();
...
$o->SayName();
Or you can build class which will be just used as data container:
class DataContainer {
public $name;
public $address;
// ...
}
// By reference this will modify object previously created
function GetProperties( &$dataContainer) // Note that & isn't necessary since PHP5
{
$dataContainer->name = "...";
}
$c = new DataContainer();
GetProperties($c);
Or you can simplify this and use array:
function GetProperties( array &$dataContainer)
{
$dataContainer['name'] = '...';
}
$data = array();
GetProperties($data);
What about first assigning the return value of getName() to a variable?
$name = getName();
If you only need one variable you can do this
function getName(){
// Some code
return 'my name is XXX';
}
function sayMyName(){
$name = getName(); // inner function generates $name value
echo $name; // results to undefined
}
sayMyName();
Otherwise you may consider using a class : http://www.php.net/manual/en/language.oop5.php
You can use references
$param = "aaa";
function f(&$param)
{
//dostuff
inner($param);
echo $param;
}
function inner(&$inner) { //do other stuff }
or use return value
function f() { echo inner(); }
function inner($param) {return $param;}
if you work on references, both functions will work on same variable, not on a copy
http://php.net/manual/en/language.references.php
the best way would be with class
<?php
class Person
{
private $name;
public function setName($name){ $this->name = $name;}
public function sayName() {echo $this->name;}
}
$person = new Person();
$person->setName("Robert");
$person->sayName();
It's good way to make it in OOP.
That what you are thinking is wrong, however you can return an array of values. For ex:
function sayMyName()
{
$result = getName(); // inner function creates an array
echo $result['name'];
}
or better an object:
class Results
{
public $name;
}
function sayMyName()
{
$result = getName(); // inner function creating an object
echo $result->name;
}
You can also do it as below.
$name = "";
function sayMyName(){
getName(); // inner function generates $name value
//set $name variable inside getName() function.
echo $name; // results to undefined
}
sayMyName();
Please use bellow code ,it will solve your problem
global $var;
You can use it anywhere within your php span.
I have a class that translate language array base. So the problem is that the language does not change base on a cookie value.
this function should set the language value but it does not. it seems that no matter what i do i always get "ar" as a self::$currlang value. how can i correct this issue?
public function _set(){
if( $_COOKIE['defaultLang'] != '' ) {
self::$currlang = $_COOKIE['defaultLang'];
} else {
//this is the default language
self::$currlang = 'ar';
}
}
here is my code
thanks for your help :)
<?php
include('../langs/english.php');
include('../langs/arabic.php');
class Translator{
private static $strs = array();
private static $currlang = "";
public function _set(){
if( $_COOKIE['defaultLang'] != '' ) {
self::$currlang = $_COOKIE['defaultLang'];
} else {
//this is the default language
self::$currlang = 'ar';
}
}
public static function loadTranslation($lang, $strs){
if (empty(self::$strs[$lang]))
self::$strs[$lang] = array();
self::$strs[$lang] = array_merge(self::$strs[$lang], $strs);
}
public static function setDefaultLang($lang){
self::$currlang = $lang;
}
public static function getDefaultLang(){
return self::$currlang;
}
public static function translate($key, $lang=""){
if ($lang == ""){
$lang = self::$currlang;
}
$str = self::$strs[$lang][$key];
if (empty($str)){
//$str = "$lang.$key";
$str = 'Language "'. $lang . '", '. $key . ' is not defined.';
}
return $str;
}
public static function freeUnused(){
foreach(self::$strs as $lang => $data){
if ($lang != self::$currlang){
$lstr = self::$strs[$lang]['langname'];
self::$strs[$lang] = array();
self::$strs[$lang]['langname'] = $lstr;
}
}
}
public static function getLangList(){
$list = array();
foreach(self::$strs as $lang => $data){
$h['name'] = $lang;
$h['desc'] = self::$strs[$lang]['langname'];
$h['current'] = $lang == self::$currlang;
$list[] = $h;
}
return $list;
}
public static function &getAllStrings($lang){
return self::$strs[$lang];
}
}
?>
The _set() magic method works with the -> operator, which works with instantiated objects. You can't use static member variables with instantiated objects, it's one or the other.