Reputation: 8924
Given a basic object like the following my inclination (based on working with AS3) is that $friend
could be interpreted $this->friend
but the PHP parser only sees $friend
as an uninitialized variable localized to the holler
function. Is there a way to access member variables without using $this->
? My goal is to discover the leanest possible syntax.
class MyBuddy
{
private $friend = true;
public function holler()
{
if ( $friend ) // <- parser won't resolve $friend to a member variable
return 'Heeeeey Buuuuuday!';
else
return null;
}
}
Update: After considering the answers given it seems that the most concise and easy to understand approach is to pass the instance variable by reference to a function level variable at the top of a function. It's a decent solution for functions which reference verbose instance variables.
// Demonstrating a simple cache which abbreviates $this->thingCollection
// to $things for the function body
public function getThing( $id, $qty )
{
$things = &$this->thingCollection; // <-- pass by reference
if ( empty($things) )
$things = [];
if ( empty($things[$id]) )
$things[ $productId ] = [];
if ( empty($things[ $id ][ $qty ]) )
$things[ $id ][ $qty ] = get_thing_from_database( $id, $qty );
return $things[ $id ][ $qty ];
}
Upvotes: 1
Views: 55
Reputation: 37065
The issue is that php doesn't consider them one in the same, thus allowing a specific method to have a local variable with that properties name. For instance:
class MyBuddy
{
private $friend = true;
public function holler($friend)
{
if ($this->friend == $friend ) // <- parser won't resolve $friend to a member variable
return 'Heeeeey Buuuuuday!';
else
return null;
}
}
define("HELL_NAW", false);
define("MMM_HMMM", true);
$hombre = new MyBuddy();
echo $hombre -> holler(HELL_NAW);
$l_jessie = new MyBuddy();
echo $l_jessie -> holler(MMM_HMMM);
So to get what you're after, you could go with:
public function holler()
{
$friend = $this ->friend;
if ($friend )
return 'Heeeeey Buuuuuday!';
else
return null;
}
But that might be called the opposite of lean. But it does also illustrate the point (and Alex's) that php isn't set up with your Responsibility Principle in mind and you'll end up doing more work to make things harder for the next guy to achieve a goal based on principle but will appear to be aesthetic to anyone else.
On the other hand, php does have the magic methods __get()
and __set()
which allow for referencing non-defined or inaccessible properties by defining how they are handled. With that, you wouldn't need to reference $this->friend
since it doesn't exist. Just reference the argument for the method (which is handy but will again just make things a cluster-bate to look at).
Upvotes: 1
Reputation: 4719
I am sympathetic to your question because I almost posted it myself. This is a case in which what you want to do is more readable to you, but won't be to another PHP developer expecting standard use of $this-> when targeting class level objects.
Upvotes: 0
Reputation: 9891
Do not invent clever workarounds that developers maintaining the code after you will have a hard time understanding. The way PHP does it is using $this, and you should embrace the conventions of language.
Upvotes: 2