dynamic calling/init a function or variable - php

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

Related

how to send variable from function to another function in a same controller with codeigniter

i have a problem with send variable data from function to another function in a same controller orderProcess :
this my controller orderProcess :
function endOrder(){
$datap['invoice_pad'] = $invoice;
$datap['date_end'] = date('d-m-Y');
$datap['total_order'] = $grt;
//$datap i want send to the function controller order()
}
function order(){
//here should be $datap accepted
}
function endOrder()
{
$datap['invoice_pad'] = $invoice;
$datap['date_end'] = date('d-m-Y');
$datap['total_order'] = $grt;
$this->order($datap);
}
function order($data){
echo $data['invoice_pad'];
echo $data['date_end'];
echo $data['total_order'];
}
function endOrder(){
$datap['invoice_pad'] = $invoice;
$datap['date_end'] = date('d-m-Y');
$datap['total_order'] = $grt;
return $datap;
}
function order(){
$datap = $this->endOrder();
}
Usually I will declare a variable and use it to pass around any data that is needed. But above answer also able to achieve what you wanted.
function __construct()
{
$this->_datap = [];
}
function endOrder()
{
$this->_datap['invoice_pad'] = $invoice;
$this->_datap['date_end'] = date('d-m-Y');
$this->_datap['total_order'] = $grt;
}
function order(){
print_r(this->_datap);
}

Echo variable which has been set in another classes function

I am trying to access the contents of a variable from another class. I have the code below, I am expecting to get 'test' returned, I get nothing.
I assume this is because it is getting $abc_rank as empty. It is required that the variable is populated in the function itself.
Therefore how can I get $abc_rank to hold that echo and output via the other class?
<?php
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
$this->abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
$go = new class2();
?>
I know I can do:
public static $abc_rank = 'test';
But the population of the variable must be in a function.
I have read some of the other related answers and can't seem to get this to work.
In class1 :
Replace $this->abc_rank = 'test'; with $this::$abc_rank='test';
($abc_rank is a static property)
In class2 :
In your display function : replace
$test = class1::$abc_rank;
echo $test;
with
$test = new class1();
echo $test::$abc_rank;
(class1 isn't static)
Full code here :
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
//$this->abc_rank = 'test';
$this::$abc_rank='test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
//$test = class1::$abc_rank;
//echo $test;
$test = new class1();
echo $test::$abc_rank;
}
}
$go = new class2();
you have to create the class1 to run the constructor of this class.
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
self::$abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
new class1();
$go = new class2();

How To Get Variable From Constructor So It Can Be Used In Other Functions?

I have this code :
<?php
class Email{
public $mandrill_host;
public function __construct() {
$this->config_ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/config.ini', true);
$this->mandrill_host = $config_ini['mandrill']['host'];
}
public function sendEmail () {
$res = $this->mandrill_host;
return $res;
}
}
$test = new Email;
echo $test->sendEmail ();
?>
and it gives me an empty result. it seems that the constructor method doesn't give the variable needed in sendEmail function. even though I already declared as public variable in class level.
how to get $this->mandrill_host from constructor so I can use it in any other method? what did I miss here?
Try
class Email{
public $mandrill_host;
public $config_ini; //you are missing this
public function __construct() {
$this->config_ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/config.ini', true);
$this->mandrill_host = $this->config_ini['mandrill']['host'];
}
public function sendEmail () {
$res = $this->mandrill_host;
return $res;
}
}
$test = new Email;
echo $test->sendEmail ();
?>

passing variables from a protected function to a public function inside the same class in php

I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;

Transient data type in php

Is there any data type in php that works like #transient of JPA(Java)?
Something like:
#transient
private $my_var;
Not really, but you can define your own serialization method and include or exclude whatever you want.
Example from the documentation: http://php.net/serializable
<?php
class obj implements Serializable {
private $data;
public function __construct() {
$this->data = "My private data";
}
public function serialize() {
return serialize($this->data);
}
public function unserialize($data) {
$this->data = unserialize($data);
}
public function getData() {
return $this->data;
}
}
$obj = new obj;
$ser = serialize($obj);
var_dump($ser);
$newobj = unserialize($ser);
var_dump($newobj->getData());
?>

Categories