I have a "getter" method like
function getStuff($stuff){
return 'something';
}
if I check it with empty($this->stuff), I always get FALSE, but I know $this->stuff returns data, because it works with echo.
and if I check it with !isset($this->stuff) I get the correct value and the condition is never executed...
here's the test code:
class FooBase{
public function __get($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter)) return $this->$getter();
throw new Exception("Property {$getter} is not defined.");
}
}
class Foo extends FooBase{
private $my_stuff;
public function getStuff(){
if(!$this->my_stuff) $this->my_stuff = 'whatever';
return $this->my_stuff;
}
}
$foo = new Foo();
echo $foo->stuff;
if(empty($foo->stuff)) echo 'but its not empty:(';
if($foo->stuff) echo 'see?';
empty() will call __isset() first, and only if it returns true will it call __get().
Implement __isset() and make it return true for every magic property that you support.
function __isset($name)
{
$getter = 'get' . ucfirst($name);
return method_exists($this, $getter);
}
Magic getters are not called when checking with empty. The value really does not exist, so empty returns true. You will need to implement __isset as well to make that work correctly.
__isset() is triggered by calling isset() or empty() on inaccessible properties.
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
PHP's magic get method is named __get(). $this->stuff will not call getStuff(). Try this:
public function __get($property) {
if ($property == 'stuff') {
return $this->getStuff();
}
}
Related
How can I assign a function to a method in a class in PHP? I tried the following:
class Something{
public function __construct(){
$functionNames = array('foo', 'bar')
$variable = 'blablabla';
foreach($functionNames as $functionName){
if(method_exists($this, $functionName))
continue;
$this->{$functionName}() = function($params){ //should create the methods "foo" and "bar"
echo $variable; //should echo 'blablabla' (I know that the variable was declared outside this function, but how can I access it anyway?)
}; //the error points to here
}
}
}
But this code gives me this error:
Fatal error: Can't use method return value in write context
Does anyone know how I can assign the anonymous function to the class method, while also still being able to access variables outside that function?
You are doing foreach($functionNames as $functionName){ which means that $functionName is a string, not an array. So, don't use $functionName[0].
method_exists takes 2 parameters. One is the object and the other is the method name. It should be:
method_exists($this, $functionName)
As for creating the function, you don't need () on the left side of the =. It should be:
$this->$functionName = function($params) use($variable){
echo $variable;
};
The use($variable) is needed to tell PHP to use that variable inside the function. That's how closures work in PHP, it's different than other languages.
So, your class should look like:
class Something{
public function __construct(){
$functionNames = array('foo', 'bar');
$variable = 'blablabla';
foreach($functionNames as $functionName){
if(method_exists($this, $functionName)){
continue;
}
$this->$functionName = function($params) use($variable){
echo $variable;
};
}
}
}
Problem here is that in this way of making functions, you are not actually creating a class method, but instead creating a class variable that contains a function.
So, you need to call it like so:
$test = new Something;
$foo = $test->foo;
$foo('abc');
You can't just do $test->foo('abc');.
EDIT: Another thing you can do is use PHP's __call "magic method". This will be ran whenever you do ->funcName(), regardless of whether the method exists or not. Using that method, you can just check to see if the method called was 'foo' or 'bar'. See this example:
class Something{
private $variable;
public function __construct(){
$this->variable = 'blablabla';
}
public function __call($name, $params=array()){
if(method_exists($this, $name)){
// This makes sure methods that *do* exist continue to work
return call_user_func(array($this, $name), $params);
}
else{
$functionNames = array('foo', 'bar');
if(in_array($name, $functionNames)){
// You called ->foo() or ->bar(), so do something
// If you'd like you can call another method in the class
echo $this->variable;
}
}
}
}
With this, now you can do the following:
$test = new Something;
$test->foo('abc'); // Will echo "blablabla"
$user = new User(1);
var_dump($user->ID);
if (empty($user->ID))
echo "empty";
// output string(2) "77" empty
So why is empty() returning true even when $user var is not empty?
The relevant parts of my User class:
class User {
protected $data = null;
public function __construct($userID) {
// sql select
$this->data = $sqlResult;
}
// ...
public function __get($name) {
if (isset($this->data[$name]))
return $this->data[$name];
else
return null;
}
}
UPDATE:
So I updated my User class and added the __isset() method
public function __isset($name) {
if (isset($this->data[$name]) && !empty($this->data[$name]))
return true;
else
return false;
}
This leads me to another problem:
When calling empty() on my not empty var empty($user->ID) it will return false, but when using isset($user->ID) on a declared var which is empty (e.g. $user->ID = '') it will also return false, because isset() will call __isset() inside the class, right?
Is there a way to fix this behaviour?
PHP notes, that I should copy the overloaded property into a local variable, which seems too much paperwork for me ;)
empty() doesn't call __get(). You need to implement __isset().
Quoting from the manual:
Note:
It is not possible to use overloaded properties in other language constructs than isset(). This means if empty() is called on an overloaded property, the overloaded method is not called.
According to the docs, you should overload __isset() for empty to work
I recently went to an interview and my code I supplied had magic functions to get and set variables. My code was as follows:
public function __get($name){
try {
return $this->$name;
} catch (Exception $e) {
throw new Exception('Trying to get a variable "'.$name.'" that does not exist.');
}
}
In the interview the guy asked me about the visibility on my variables, I had private ones set but these were now accessible by using magic functions. Essentially I failed the interview on this point, so I wanted to understand more. I was following a tutorial from PHP Master and found a different __get, I have tried to break it but it seems to work, but in a strange way.
I call __get('test') to get my variable _test but if it is set to private it calls itself again and tells me that it cannot access __test. I do not really understand why it calls itself again.
public function __get($name)
{
$field = '_' . strtolower($name);
if (!property_exists($this, $field)){
throw new \InvalidArgumentException(
"Getting the field '$field' is not valid for this entity"
);
}
$accessor = 'get' . ucfirst(strtolower($name));
return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
$this->$accessor() : $this->$field;
}
Can anyone give me some pointers on proper use of __get and __set when using visibility in a class and why this function would call itself again.
I have read the other posts here but I am still struggling with this concept.
I just bumped into this question and there is a little thing that may be worth clarifying:
I do not really understand why it calls itself again.
The code is not calling itself again but trying to execute a custom getter if there is one defined. Let me break down the method execution:
public function __get($name)
{
As already explained in the other answers and here, the __get() magic method is called when you are trying to access a property that is not declared or not visible in the calling scope.
$field = '_' . strtolower($name);
if (!property_exists($this, $field)){
throw new \InvalidArgumentException(
"Getting the field '$field' is not valid for this entity"
);
}
Here it just checks that a property with an underscore pre-appended exists in the class definition. If it doesn't, an exception is thrown.
$accessor = 'get' . ucfirst(strtolower($name));
Here it creates the name of the getter to call if it exists. Thus, if you try to access a property named email and there is a private member called _email the $accessor variable will now hold the 'getEmail' string.
return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
$this->$accessor() : $this->$field;
The final part is a bit cryiptic, since many things are happening in one line:
method_exists($this, $accessor). Checks if the receiver ($this) has a method with $accessor name (in our example, getEmail).
is_callable(array($this, $accessor)). Checks that the getter can be called.
If both conditions are met, the custom getter is called and its return value is returned ($this->$accessor()). If not, the property contents are returned ($this->$field).
As an example consider this class definition:
class AccessorsExample
{
private $_test1 = "One";
private $_test2 = "Two";
public function getTest2()
{
echo "Calling the getter\n";
return $this->_test2;
}
public function __get($name)
{
$field = '_' . strtolower($name);
if (!property_exists($this, $field)){
throw new \InvalidArgumentException(
"Getting the field '$field' is not valid for this entity"
);
}
$accessor = 'get' . ucfirst(strtolower($name));
return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
$this->$accessor() : $this->$field;
}
}
and then run:
$example = new AccessorsExample();
echo $example->test1 . "\n";
echo $example->test2 . "\n";
You should see:
One
Calling the getter
Two
HTH
I find it's better to be explicit when allowing access to properties via __get(). This way you can still have truly private members, and you don't run the risk of accidentally exposing things you add later.
class Foo
{
// readonly
private $foo;
private $bar;
// truly private
private $baz;
public function __get($var)
{
switch ($var)
{
// readonly access to foo and bar, but not baz
case 'foo':
case 'bar':
return $this->$var;
// readonly dynamically generated property
case 'buzz':
return $this->buzz();
default:
throw new InvalidPropertyException($var);
}
}
public function __isset($var)
{
switch ($var)
{
// return true for foo, bar and buzz so functions like isset()
// and empty() work as expected
case 'foo':
case 'bar':
case 'buzz':
return true;
default:
return false;
}
}
// dynamic readonly property implementation
private function buzz()
{
// calculate and return something which depends on other private properties
}
}
I don't get exactl what is your problem, but for example this code works
<?php
class foo {
private $_test = "my";
public function __get($name)
{
$field = '_' . strtolower($name);
if (!property_exists($this, $field)){
throw new InvalidArgumentException(
"Getting the field '$field' is not valid for this entity"
);
}
$accessor = 'get' . ucfirst(strtolower($name));
return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
$this->$accessor() : $this->$field;
}
}
$foo = new foo();
echo $foo->test;
as you can check here (http://codepad.org/jmkvHiDe).
Magic methods like __get() will be called, when you try to access a private property exactly as they would be called to access a non existing property, of course if you set a property as "private" and then the user can access the variable through a magic method, why set the property as private in the first place?
Instead of $this->$name;
Use something like $this->protected_values[$name];
public function __get($name){
try {
return $this->$name;
} catch (Exception $e) {
throw new Exception('Trying to get a variable "'.$name.'" that does not exist.');
}
}
A couple of problems with this method as it stands, without seeing the rest of the code:
It allows unrestricted public read access to all private and protected properties inside the class. Except in special cases this is usually undesirable as it does defeat the object of class member visibility. As mentioned earlier, access should be restricted, either by checking against an allowed list (as in Rob Agar's answer) or by checking for a defined getter (as in the OP's question).
An exception is not normally thrown when accessing an undefined property (unless you have a custom error handler that is set to do so). Accessing an undefined property normally triggers an E_NOTICE, so your method would not trap this. You should first validate that $name does actually exist.
I'm trying to make functions like empty() and isset() work with data returned by methods.
What I have so far:
abstract class FooBase{
public function __isset($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return isset($this->$getter()); // not working :(
// Fatal error: Can't use method return value in write context
}
public function __get($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return $this->$getter();
}
public function __set($name, $value){
$setter = 'set'.ucfirst($name);
if(method_exists($this, $setter))
return $this->$setter($value);
}
public function __call($name, $arguments){
$caller = 'call'.ucfirst($name);
if(method_exists($this, $caller)) return $this->$caller($arguments);
}
}
the usage:
class Foo extends FooBase{
private $my_stuff;
public function getStuff(){
return $this->my_stuff;
}
public function setStuff($stuff){
$this->my_stuff = $stuff;
}
}
$foo = new Foo();
if(empty($foo->stuff)) echo "empty() works! \n"; else "empty() doesn't work:( \n";
$foo->stuff = 'something';
if(empty($foo->stuff)) echo "empty() doesn't work:( \n"; else "empty() works! \n";
http://codepad.org/QuPNLYXP
How can I make it so empty/isset return true/false if:
my_stuff above is not set, or has a empty or zero value in case of empty()
the method doesn't exist (not sure if neeed, because I think you get a fatal error anyway)
?
public function __isset($name){
$getter = 'get'.ucfirst($name);
return method_exists($this, $getter) && !is_null($this->$getter());
}
This check whether or not $getter() exists (if it does not exist, it's assumed that the property also does not exist) and returns a non-null value. So NULL will cause it to return false, as you would expect after reading the php manual for isset().
A bit more option not to depend on getter
public function __isset($name)
{
$getter = 'get' . ucfirst($name);
if (method_exists($this, $getter)) {
return !is_null($this->$getter());
} else {
return isset($this->$name);
}
}
Your code returns error because of these lines:
if(method_exists($this, $getter))
return isset($this->$getter());
You can just replace it with:
if (!method_exists($this), $getter) {
return false; // method does not exist, assume no property
}
$getter_result = $this->$getter();
return isset($getter_result);
and it will return false if the getter is not defined or it returns NULL. I propose you should better think of the way you determine some property is set or not.
The above code is also assuming that you are creating getters for all of your properties, thus when there is no getter, the property is assumed as not set.
Also, why are you using getters? They seem to be some overkill here.
I want to use magic function __set() and __get() for storing SQL data inside a php5 class and I get some strange issue using them inside a function:
Works:
if (!isset($this->sPrimaryKey) || !isset($this->sTable))
return false;
$id = $this->{$this->sPrimaryKey};
if (empty($id))
return false;
echo 'yaay!';
Does not work:
if (!isset($this->sPrimaryKey) || !isset($this->sTable))
return false;
if (empty($this->{$this->sPrimaryKey}))
return false;
echo 'yaay!';
would this be a php bug?
empty() first* calls the __isset() method and only if it returns true the __get() method. i.e. your class has to implement __isset() as well.
E.g.
<?php
class Foo {
public function __isset($name) {
echo "Foo:__isset($name) invoked\n";
return 'bar'===$name;
}
public function __get($name) {
echo "Foo:__get($name) invoked\n";
return 'lalala';
}
}
$foo = new Foo;
var_dump(empty($foo->dummy));
var_dump(empty($foo->bar));
prints
Foo:__isset(dummy) invoked
bool(true)
Foo:__isset(bar) invoked
Foo:__get(bar) invoked
bool(false)
* edit: if it can't "directly" find an accessible property in the object's property hashtable.