0

Nowadays i'm trying to develop a php framework for own customers. In my app, I want to pass $instance->propertyName as parameter. But don't need $instance->propertyName value. Only I want to use propertyName part as string value. Can I take propertyName as string?

For example if I have an object $bar of the class

class foo
{
    public $someProperty1;
    public $someProperty2;
}

$bar = new foo();

and if I have another class like

class anotherClass
{
    public function someMethod($arg)
    {
        //I need the property name that provide the $arg value in this place
    }
}

and when I run this code

$someObject = new anotherClass();
$someObject->someMethod($bar->someProperty1); //I want to know the name of property that provide a value to the someMethod method (the 'someProperty1' in this case)

then I want to know inside the method someMethod in anotherClass class the name of the property that provide the $arg value. As the result for example above I want to get a string someProperty1.

zajonc
  • 1,935
  • 5
  • 20
  • 25
ireaf
  • 3
  • 3
  • `$foo = $obj->foo; echo $foo;` If you want to get fancy, then you could do `$foo &= $obj->foo`, so `$foo` updates whenever you change `$obj->foo` – Marc B Jul 26 '16 at 19:54
  • I don't know what he means, but I don't think that is it. Do you mean using $instance->$variablePropertyName. Or do you mean converting $instance->propertyName to $propertyName as a variable containing the string "propertName"? – Anees Saban Jul 26 '16 at 20:03
  • Thank you but this is not solved my problem. I tried it but result s 0 on my screen. – ireaf Jul 26 '16 at 20:03
  • Can you try to explain in more detail what you are trying to achieve? – Anees Saban Jul 26 '16 at 20:05
  • I think you'll really need to explain more specifically what you're trying to do and show an example of the actual code you're using. – Don't Panic Jul 26 '16 at 20:08
  • This my concrete example. My class properties equal my table fields. I am developing a class for table query. With my classes and helpers i am creating a dynamic sql. For example. $result = $instance->select("*")->from("students")->where("age > 20")->orderBy("age"); In this example not exacly my project but it seems. Now i want to change this code as below $result = $instance->select("*")->from("students")->where($entity->age > 20)->orderBy($entity->age); To do this work, i need only "age" from this: $entity->age. Or can you suggest any useful method for my project. Thank you. – ireaf Jul 27 '16 at 19:33

1 Answers1

0

There is no simple way to achive this. You want to know something like this

function foo($bar)
{
    //here you want to know the name of the variable that passing the value
}

$x = 5;
$y = 5;

foo($x); //here you want to know the name 'x' of the variable
foo($y); //here you want to know the name 'y' of the variable

In both case function will get only information about value 5 and nothig about name of the variable provide the value.

If you want to know the name of the variable (or object property) that provides value into method then you can use something very inefficient like this

class someClass
{
    public $someProperty;
}

class otherClass
{
    public function setSomething($arg)
    {
        $trace = debug_backtrace();
        $vLine = file($trace[0]['file']);
        $fLine = $vLine[$trace[0]['line'] - 1];
        preg_match("/(.*?)\((.*?)->(.*?)\)/i", $fLine, $match);

        var_dump($match[3], $arg);
    }
}

$t = new someClass();
$t->someProperty = 5;

$o = new otherClass();
$o->setSomething($t->someProperty);

The var_dump in the setSomething method will return the name and the value of the property

string(12) "someProperty" int(5)

This solution is based on idea from this post https://stackoverflow.com/a/404637/4662836

Community
  • 1
  • 1
zajonc
  • 1,935
  • 5
  • 20
  • 25