Pass variables from class instance to its extended method - php

I'm trying to pass a variable to a method in an extended class, but it's not working.
Here's the sample code:
class set2 extends set1
{
function Body($variable) {
}
}
$start2 = new set2();
$start2->Body('some text');
The last line is the part I'm trying to get to work. I'm not sure if I should have a constructor instead to do it or how it's best to get it to work.
I figured it out. I just added a public variable instead and passed its value like this:
class set2 extends set1
{
public $variable = NULL;
function Body() {
echo $this->variable;
}
}
$start2 = new set2();
$start2->variable = 'Some Text';

Three different ways of doing what I think you're trying to do:
class set1
{
protected $headVariable;
function Head() {
echo $this->headVariable;
}
function Body($variable) {
echo $variable;
}
function Foot() {
echo static::$footVariable;
}
}
class set2 extends set1
{
protected static $footVariable;
function Head($variable) {
$this->headVariable = $variable;
parent::Head();
}
function Body($variable) {
parent::Body($variable);
}
function Foot($variable) {
self::$footVariable = $variable;
parent::Foot();
}
}
$start2 = new set2();
$start2->Head('some text');
$start2->Body('some more text');
$start2->Foot('yet more text');

Related

PHP - ReflectionFunction - Function Test::test_function() does not exist

I could use RefexionFunction outside of a class, but inside a class I get an exception.
Fatal error: Uncaught ReflectionException: Function Test::test_function() does not exist in test.php.
<?php
function parameters($functionName,$args){
$f = new ReflectionFunction($functionName);
....
}
class Test{
public function test_functionA($abc,$d,$e,$f) {
parameters(__METHOD__,func_get_args());
}
protected function test_functionB($abc,$d,$e,$f) {
parameters(__METHOD__,func_get_args());
}
private function test_functionC($abc,$d,$e,$f) {
parameters(__METHOD__,func_get_args());
}
}
$test = new Test();
$test->test_function('something',1,2,array(123,456));
?>
Appreciate your help.
Your error:
Fatal error: Uncaught ReflectionException: Function Test::test_function() does not exist in test.php.
Doesn't refer to the function name quite as you expect it to.
ReflectionClass docs says this:
The ReflectionClass class reports information about a class.
ref: https://secure.php.net/manual/en/class.reflectionclass.php
You want to use a combination of methods available in that class to get information about the passed method like this:
public function parameters($class, $fnc)
{
$f = new ReflectionClass($class);
if ($f->hasMethod($fnc)) {
return 'howdy folks';
} else {
return 'not so howdy folks';
}
}
You first pass the class before checking if the function exists. You can then use the built-in function hasMethod to check if the function exists. You then use the parameters function like this:
public function testFunction()
{
return $this->helper->parameters(__CLASS__, __FUNCTION__);
}
All together the code looks like this:
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
class paramsHelper
{
public function parameters($class, $fnc)
{
$f = new ReflectionClass($class);
$f->getMethod($fnc);
if ($f->hasMethod($fnc)) {
return 'howdy folks';
} else {
return 'not so howdy folks';
}
return $f;
}
}
class Test
{
protected $helper;
public function __construct($helper)
{
$this->helper = $helper;
}
public function testFunction()
{
return $this->helper->parameters(__CLASS__, __FUNCTION__);
}
}
$test = new Test(new paramsHelper());
echo '<pre>';
print_r($test->testFunction());
echo '</pre>';
One of your other problems is that __METHOD__ actually returns a string like this: Test::testFunction not testFunction - hence my use of __FUNCTION__ instead.
Edit:
To get the parameters of the passed method, change your parameters method to this:
class paramsHelper
{
public function getMethodParameters($class, $fnc)
{
$f = new ReflectionMethod($class, $fnc);
echo '<pre>';
print_r($f->getParameters());
echo '</pre>';
}
}
This uses ReflectionMethod in place of ReflectionClass - this is more inline with your intended use.
ref: https://secure.php.net/manual/en/class.reflectionmethod.php
use:
class paramsHelper
{
public function getMethodParameters($class, $fnc)
{
$f = new ReflectionMethod($class, $fnc);
echo '<pre>';
print_r($f->getParameters());
echo '</pre>';
}
}
class Test
{
protected $helper;
public function __construct($helper)
{
$this->helper = $helper;
}
public function testFunction($a = '', $b = 1, $c = 3)
{
return $this->helper->parameters(__CLASS__, __FUNCTION__);
}
}
$test = new Test(new paramsHelper());
echo '<pre>';
print_r($test->testFunction());
echo '</pre>';

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 extract variable from one function into another in same class in php

I want to use variable value from one function into another function of same class. I am using abstract class using which I am declaring variable as global indirectly. I can not declare variable as global in the class. My demo code is as follows:
<?php
abstract class abc
{
protected $te;
}
class test extends abc
{
public function team()
{
$te = 5;
$this->te += 100;
}
public function tee()
{
$tee = 51;
return $this->te;
}
}
$obj = new test();
echo $obj->tee();
//echo test::tee();
?>
Is this possible that I can echo 105 as answer there?
My main motive is I want to learn that how to get variable value from one function into another using without declaring that global in the same class Please let me know Is this possible OR I need to delete my question ?
<?php
abstract class abc
{
protected $te;
}
class test extends abc
{
public function __construct() {
$this->te = 5;
}
public function team()
{
$this->te += 100;
}
public function tee()
{
return $this->te;
}
}
$obj = new test();
$obj->team();
echo $obj->tee();
-- edit: to make at least some use of the abstract "feature":
<?php
abstract class abc
{
protected $te;
abstract public function team();
public function tee()
{
return $this->te;
}
}
class test extends abc
{
public function __construct() {
$this->te = 5;
}
public function team()
{
$this->te += 100;
}
}
$obj = new test();
$obj->team();
echo $obj->tee();
-- edi2: since you've asked whether you must invoke team (and then deleted that comment):
<?php
abstract class abc
{
protected $te;
abstract public function team();
public function tee()
{
$this->team();
return $this->te;
}
}
class test extends abc
{
public function __construct() {
$this->te = 5;
}
public function team()
{
$this->te += 100;
}
}
$obj = new test();
echo $obj->tee();
So, yes, it has to be invoked somewhere. But depending on what you're trying to achieve there are numerous ways to do so.
Each property of the class can be accessed by each method of the same class. So you can create methods which are working with the same property. And you don't need to create parent abstract class.
class test
{
protected $te = 5;
public function team()
{
$this->te += 100;
}
public function tee()
{
return $this->te;
}
}
$obj = new test();
$obj->team();
echo $obj->tee();

PHP: How do I access a local varibale of a function in class from another function

So i have a php class and i'm having a small snarl up. imagine a class as one shown below
<?php
class Foo
{
public function __construct()
{
$this->bar1();
}
public function bar1()
{
$myvar = 'Booya!';
return 'Something different';
}
public function bar2()
{
//get value of $myvar from bar1()
}
}
$new_foo = new Foo();
$new_foo->bar2();
?>
Question is,
how do I access the variable $myvar from bar1() keeping in mind that bar1() returns something different.
You would do something like this... Everything has been explained through comments next to the code.
<?php
class Foo
{
private $myvar; //<---- Declare the variable !
public function __construct()
{
$this->bar1();
}
public function bar1()
{
$this->myvar = 'Booya!'; //<---- Use this $this keyword
//return 'Something different';//<--- Comment it.. Its not required !
}
public function bar2()
{
return $this->myvar; //<----- You need to add the return keyword
}
}
$new_foo = new Foo();
echo $new_foo->bar2(); //"prints" Booya!
<?php
class Foo
{
var $myvar;
public function __construct()
{
$this->bar1();
}
public function bar1()
{
$this->myvar = 'Booya!';
return 'Something different';
}
public function bar2()
{
//get value of $myvar from bar1()
echo $this->myvar;
}
}
$new_foo = new Foo();
$new_foo->bar2();
?>
You should set it as a class variable first, then access it using $this
You cannot do that directly, only you can do without altering bar1() return value is create a
class variable for saving the value of this data
In class definition add
private $saved_data;
In bar1():
$myvar = 'Booya!';
$this->saved_data = $myvar;
And in bar2()
$myvar_from_bar1 = $this->saved_data
Use class variables like:
$this->myvar = 'Booya!';
Now the variable myvar will be store in the class and can be requested or altered in other methods.

PHP How to distinguish $this pointer in the inheritance chain?

Please look at the following code snipped
class A
{
function __get($name)
{
if ($name == 'service') {
return new Proxy($this);
}
}
function render()
{
echo 'Rendering A class : ' . $this->service->get('title');
}
protected function resourceFile()
{
return 'A.res';
}
}
class B extends A
{
protected function resourceFile()
{
return 'B.res';
}
function render()
{
parent::render();
echo 'Rendering B class : ' . $this->service->get('title');
}
}
class Proxy
{
private $mSite = null;
public function __construct($site)
{
$this->mSite = $site;
}
public function get($key)
{
// problem here
}
}
// in the main script
$obj = new B();
$obj->render();
Question is: in method 'get' of class 'Proxy', how I extract the corresponding resource file name (resourceFile returns the name) by using only $mSite (object pointer)?
What about:
public function get($key)
{
$file = $this->mSite->resourceFile();
}
But this requires A::resourceFile() to be public otherwise you cannot access the method from outside the object scope - that's what access modifiers have been designed for.
EDIT:
OK - now I think I do understand, what you want to achieve. The following example should demonstrate the desired behavior:
class A
{
private function _method()
{
return 'A';
}
public function render()
{
echo $this->_method();
}
}
class B extends A
{
private function _method()
{
return 'B';
}
public function render()
{
parent::render();
echo $this->_method();
}
}
$b = new B();
$b->render(); // outputs AB
But if you ask me - I think you should think about your design as the solution seems somewhat hacky and hard to understand for someone looking at the code.

Categories