0

Is it any way to skip $this in onject? Having this class:

Class klas
{
  private $var1;
  private $var2;

  public function makeIt()
  {
    echo $this->var1;

  }

}

is it way to do this in something like this way:

Class klas
{
  private $var1;
  private $var2;

  public function makeIt()
  {
    echo $var1;

  }

}

Maybe is somewhere some kind of magic method or domething to do this?

19
  • 3
    actually $this does the magic for you since it refers to the current object and using it you can access member data. Commented Mar 10, 2014 at 15:43
  • 3
    No. There's no easier way. $classInstance->property is how you access regular non-static values in a class. $this just refers to the current instance of the class. Commented Mar 10, 2014 at 15:43
  • 2
    It might be better if you give an overview of what you are trying achieve. It seems you might need to use static methods but need clarification on the context. Commented Mar 10, 2014 at 15:44
  • 4
    No, that's not a great idea. sparing those 3 characters just doesn't worth it. Readability comes first - "Always write code as if the person who will be maintaining it is a psychotic serial killer who knows where you live". + en.wikipedia.org/wiki/Principle_of_least_astonishment Commented Mar 10, 2014 at 15:54
  • 5
    This question appears to be off-topic because you should have read manual instead. Commented Mar 10, 2014 at 16:43

3 Answers 3

3

If you do not need to modify the values, only use them, you can use extract in combination with get_object_vars to create local copies.

This will define any properties within the current symbol table, letting you refer to them as local variables. Note that any changes to them will not affect the corresponding object properties, as these are NOT references, but new copies of the variables.

<?php
Class klas
{
  public $foo = 'world';
  private $bar = 'my code';

  public function makeIt()
  {
      extract(get_object_vars($this));
      print 'Hello '.$foo.', velkommen to '.$bar;
      $foo = 'Dave';
  }

}

$inst = new klas();
$inst->makeIt();

print 'Hello '.$inst->foo; // not "Hello Dave"

Try it: http://codepad.viper-7.com/eKr1v7

You can also cast the object as an array instead of using get_object_vars:

$vars = (array)$this;
extract($vars);

The documentation of extract mentions a EXTR_REFS flag, which should extract references into the local symbol table. When you cast the object as an array, the resulting array is said to be references (in comments in PHP docs), but, I was not able to reproduce this on codepad, even though the version is comparable to that referenced in the claim. You can give it a shot on your installation, your mileage may vary.

Documentation

Sign up to request clarification or add additional context in comments.

Comments

2

Without pointlessly addling boiler-plate code to the local scope...

No, it's not possible, PHP doesn't support such a mechanism.

More importantly, it's totally pointless and bad practice, as many good reputation users already mentioned in the comments.

14 Comments

I disagree with this answer from a technical perspective -- extract and get_object_vars are supported mechanisms, and possible.
While we probably all agree that it's bad practice, he asked if there was a way to shorten $this->attribute, presumably because he wants to save time typing it out. Both my answer and Chris' present a way to do this. The question does not specify how exactly it should be achieved or whether or not only good practice shall be posted. There's nothing wrong with either of those answers, but yours is just plain wrong. stating it's bad practice has been done in the comments, where it belongs.
@Chris: extract is wrong, because it will create a copy of the variables. So it's not "shortening", but instead creating a slightly different variable. Proof. So no, this is the correct answer, as it talks about pointless boilerplate, and indicates that it's bad practice in the first place.
In what point do we disagree? Does the question not ask for "some kind of magic method or [s]omething"? Does either my or Chris' answer not give a way to "skip $this"? Do those not imply that "there is no way" is a false statement? Does a comment about how OP's goal is bad practice not belong in the comments rather than an answer? Is it not wrong to downvote a correct and working answer because you think what's being asked in the question should not be done? Ignoring even the fact the answer does not only point out that it's wrong, but also explain why (in addition to answering it)?
@ircmaxell woah, woah, woah. It does too work. I am pretty explicit in my answer that these are not references, but copies. I guess I could word it even more explicitly, but the code does exactly what I say it does. The debate about whether you'd ever want to do this is open, I suppose (I would not), but as I mentioned, you can find the technique I am using discussed in upvoted comments right in the PHP docs.
|
1

There is a way to save a few characters if you have to type it a lot. At the start of the function, assign

$r = $this;

and from there on, use $r->val. This saves a little time typing out stuff. I chose r because it's right under the 4 key, e works just as well (or, really, any key you want).

However, note that this is a bad idea in general, mainly for 2 reasons.

  1. You're assigning a new variable. Since it's only a reference, this means a tiny bit of additional memory used up, but it also slightly increases computation time because PHP handles $this more efficiently than assigned variables.
  2. Your code becomes less readable. While the "pure" version you suggested in the question is probably more or less readable depending on your background (several languages do it that way), the native PHP way is very clear, especially because nearly every editor will highlight $this. It removes all possibility for confusion as it shows, without room for interpretation, that the current object is the one whose attributes or methods you're accessing. It also shows very clearly that you're nor handling some local variable (local to the function call, or more specifically, to this instance of the function call), but rather an attribute of the object the method is called on.

So, what it comes down to, is that yes, it is possible in a tricky way, but you shouldn't do it.

4 Comments

You're actually not assigning a new variable as all objects are references by default.
Doesn't it store the reference pointer though? I honestly don't know how the interpreter works internally, does it just compile all references to an object the same way, no matter what they're named?
You'd essentially only use a tiny memory stamp because of the reference (the variable named $r points to the variable in this part of the memory (the same as $this)). Otherwise they're a reference to the same variable.
Yep - that seems much better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.