How do class methods return in php - php

I'm not particularly familiar with php, and I'm having trouble understanding what's happening. Using a condensed skeleton of what I have:
class Helper
{
public function __construct($value)
{
$this->value = $value;
//etc
}
private function prefix($val)
{
return '1234' . $val;
}
private function otherFunction()
{
$this->value->someFunction(function ($err, $result) {
if($err !== null) {
echo $err->getMessage();
}
return $result;
});
}
public function help()
{
echo $this->prefix('5678'); //outputs 12345678
echo is_null($this->otherFunction()); //outputs 1
}
}
Why does otherFunction return null? I can echo $result right before the return and see what I'm expecting, but it's null afterwards.

Your otherFunction() doesn't return anything. Probably you echo $result inside of the callback function passed to $this->value->someFunction().
Add return just before $this->value->someFunction(... to return the value from otherFunction().

Related

How to check returned value to which function it belogns

Say I have to similar function :
public function auth(){
return $someResponse;
}
public function collect(){
return $someOtherResponse
}
Question : When one of the response get passed to another class, is there any way to check which function returned the response ?
In a purely object-oriented way, wanting to attach information to a value is akin to wrapping it into a container possessing context information, such as:
class ValueWithContext {
private $value;
private $context;
public function __construct($value, $context) {
$this->value = $value;
$this->context = $context;
}
public value() {
return $this->value;
}
public context() {
return $this->context;
}
}
You can use it like this:
function auth()
{
return new ValueWithContext($someresponse, "auth");
}
function collect()
{
return new ValueWithContext($someotherrpesonse, "collect");
}
This forces you to be explicit about the context attached to the value, which has the benefit of protecting you from accidental renamings of the functions themselves.
As per my comment, using arrays in the return will give you a viable solution to this.
It will allow a way to see what has been done;
function auth()
{
return (array("auth" => $someresponse));
}
function collect()
{
return (array("collect" => $someotherrpesonse));
}
class myClass
{
function doSomething($type)
{
if (function_exists($type))
{
$result = $type();
if (isset($result['auth']))
{
// Auth Used
$auth_result = $result['auth'];
}
else if (isset($result['collect']))
{
// Collect used
$collect_result = $result['collect'];
}
}
}
}
It can also give you a way to fail by having a return array("fail" => "fail reason")
As comments say also, you can just check based on function name;
class myClass
{
function doSomething($type)
{
switch ($type)
{
case "auth" :
{
$result = auth();
break;
}
case "collect" :
{
$result = collect();
break;
}
default :
{
// Some error occurred?
}
}
}
}
Either way works and is perfectly valid!
Letting the two user defined functions auth() & collect() call a common function which makes a call to debug_backtrace() function should do the trick.
function setBackTrace(){
$backTraceData = debug_backtrace();
$traceObject = array_reduce($backTraceData, function ($str, $val2) {
if (trim($str) === "") {
return $val2['function'];
}
return $str . " -> " . $val2['function'];
});
return $traceObject;
}
function getfunctionDo1(){
return setBackTrace();
}
function getfunctionDo2(){
return setBackTrace();
}
class DoSomething {
static function callfunctionTodo($type){
return (($type === 1) ? getfunctionDo1() : getfunctionDo2());
}
}
echo DoSomething::callfunctionTodo(1);
echo "<br/>";
echo DoSomething::callfunctionTodo(2);
/*Output
setBackTrace -> getfunctionDo1 -> callfunctionTodo
setBackTrace -> getfunctionDo2 -> callfunctionTodo
*/
The above function would output the which function returned the response

PHP function in a function

I am trying to build a function that will call another function.
For example, if I have an array full of function names to call, is it possible to call a function for every array value without writing it in a script?
Example:
function email($val=NULL) {
if($val)
$this->_email = $val;
else
return $this->_email;
}
function fname($val=NULL) {
if($val)
$this->_fname = $val;
else
return $this->_fname;
}
For email, fname, etc.
But I want to have it like:
function contr_val($key,$val) {
function $key($val=NULL) {
if($val)
$this->_$key = $val;
else
return $this->_$key;
}
function $key($val="hallo");
}
And call it with:
contr_val("email", "test")
You're really trying to create member variables dynamically and retrieve their values. This is what __get() and __set() are for.
Here's how you could use it:
class TestClass {
var $data = array();
public function __set($n, $v) { $this->data[$n] = $v; }
public function __get($n) {
return (isset($this->data[$n]) ? $this->data[$n] : null);
}
public function contr_val($k, $v = NULL) {
if ($v)
$this->$k = $v;
else
return $this->$k;
}
};
$sherp = new TestClass;
$sherp->contr_val("Herp", "Derp");
echo "Herp is: " . $sherp->contr_val("Herp") . "\n";
echo "Narp is: " . $sherp->contr_val("Narp") . "\n";
Something like this:
/*
Input: $val - any value
$varname - the variable name, for instance: _email
*/
function checkValue($val=NULL, $varname) {
if($val)
$this->$var = $val;
else
return $this->$var;
}
checkValue("hello", "_email");
checkValue("hello2", "_name");
If you are doing this for a class, consider using PHP's magic methods __get() and
__set().
In an array full of function names, this calls every function that exists.
ghoti#pc:~$ cat functest.php
#!/usr/local/bin/php
<?php
function one() { print "one\n"; }
function two() { print "two\n"; }
function three() { print "three\n"; }
$a=array( "one", "two", "three", "four" );
foreach ($a as $item) {
if (function_exists($item)) {
$item();
} else {
print "No such function: $item\n";
}
}
ghoti#pc:~$ ./functest.php
one
two
three
No such function: four
ghoti#pc:~$
You need to check if the function exists or not:
function contr_val($key,$val) {
if (!function_exists($key)) {
function $key($val=NULL) {
if ($val)
$this->_$key = $val;
}
}
else {
return $this->_$key;
}
}

PHP __set variable while __get passes by reference

I think its quite a simple question but not sure.
I have a class:
<?PHP
class PropertyTest {
private $data = array();
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
public function __isset($name) {
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
public function __unset($name) {
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
public function getHidden() {
return $this->hidden;
}
}
?>
Not sure why but the 'code' block is annoying as hell, anyway.
Just the basic magic get set really. But when I change the __get to pass by reference I cant do this anymore:
$object->$variableName = $variableValue;
I'm not sure why although I assume because it checks if it exists but since it has to return something by reference it will fail to do so if it doesn't exists to begin with. The set function wont be called probably and even if I return a fake value it would never call the set function cause it 'already exists/has a value'.
Am I understanding this correctly? If so, Is there a work around? If not how does it work and is there a workaround?
Unless I'm missing something it's working fine for me
<?php
class PropertyTest
{
private $data = array();
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function &__get($name)
{
if(array_key_exists($name, $this->data))
return $this->data[$name];
return null;
}
public function __isset($name)
{
return isset($this->data[$name]);
}
public function __unset($name)
{
unset($this->data[$name]);
}
public function getHidden()
{
return $this->hidden;
}
}
$oPropTest = new PropertyTest();
$sField = 'my-field';
$oPropTest->$sField = 5;
var_dump($oPropTest);
Outputs:
bash-3.2$ php test-get-set.php
object(PropertyTest)#1 (1) {
["data":"PropertyTest":private]=>
array(1) {
["my-field"]=>
int(5)
}
}
One tweak I'd suggest for your __get implementation is to leverage the __isset method rather than re-implement the check (doing it 2 different ways as you are)
public function __get($name)
{
if($this->__isset($name))
return $this->data[$name];
return null;
}
Regarding the idea of return-by-reference from __get; it will work, but be useless on anything but public attributes, since private and protected attributes won't be settable directly through a reference outside the class scope.
$oPropTest = new PropertyTest();
$sField = 'my-field';
$oPropTest->$sField = 5; // sets $oPropTest->my-field to 5 (effectively)
$iRef = $oPropTest->$sField; // $iRef is a reference to $oPropTest->my-field
$iRef = 6; // this will not set $oPropTest->my-field since it's private

Returning an instance of a node itself in a php tree class

So I'm trying to create a tree structure in PHP. I don't know if that's possible and I'm not all that great with PHP so this is difficult for me.
The code I have so far is (important stuff only, extra code has been cut out):
abstract class tree_node {
protected $_child_refs = array();
abstract public function add_child($arg);
public function count() {
return count($this->_child_refs);
}
public function get_deepest_children() {
if ($this->count() === 0) {
return $this;
} else {
foreach ($this->_child_refs as $child_ref) {
$deepest[] = $child_ref->get_deepest_children();
}
}
}
abstract public function __construct();
}
class data_node extends tree_node {
private $_data = "";
public function add_child($data) {
$new_child = new data_node($data);
$this->_child_refs[] = $new_child;
}
public function __construct($data) {
$this->_data = $data;
}
}
$foo = new data_node("foo");
$foo->add_child("bar");
var_dump($foo->get_deepest_children());
This code should return a data_node with "bar" as the data but instead I get NULL. What's wrong with "return $this"? Is that not the proper way to return an instance of a class itself?
Also, feel free to critique this code/tell me I'm doing this completely wrong. I want to keep tree functions separate from functions specific to the data stored in the tree, which is why I split it up into two classes, but if you think that's a bad idea tell me.
This:
public function get_deepest_children() {
if ($this->count() === 0) {
return $this;
} else {
foreach ($this->_child_refs as $child_ref) {
$deepest[] = $child_ref->get_deepest_children();
}
}
}
Should be something like this:
public function get_deepest_children() {
if ($this->count() === 0) {
return array($this);
}
$deepest = array();
foreach ($this->_child_refs as $child_ref) {
$deepest = array_merge($deepest,$child_ref->get_deepest_children());
}
return $deepest;
}
Your get_deepest_children isn't always returning the same type of value. In the base case it will return a node object, in the other cases it extends the $deepest list but doesn't return any value.
<?php
public function get_deepest_children() {
if ($this->count() === 0) {
return $this;
} else {
foreach ($this->_child_refs as $child_ref) {
$deepest[] = $child_ref->get_deepest_children();
}
}
}
I would modify it like this:
<?php
public function get_deepest_children() {
if ($this->count() === 0) {
return Array($this);
} else {
$deepest = Array();
foreach ($this->_child_refs as $child_ref) {
$deepest = array_merge($deepest, $child_ref->get_deepest_children());
}
return $deepest;
}
}
You should be able to see how it now always it returns an array of all of the deepest nodes (usually called leaf nodes) below/including the current one. This way we can array_merge the consecutive results to get the final ones.

is it possible check if given function is executing within other function or method?

function echo_parent_func() {
echo // so what now ?
}
function somefunc() {
echo_parent_func();
}
somefunc(); //should echo string 'somefunc'
Is that even possible with php ?
function get_caller_method()
{
$traces = debug_backtrace();
if (isset($traces[2]))
{
return $traces[2]['function'];
}
return null;
}
function echo_parent_func() {
echo get_caller_method();
}
function somefunc() {
echo_parent_func();
}
somefunc(); //should echo string 'somefunc'
Source
EDIT Just found this answer too:

Categories