1

In JS, when defining an object, if one of the object properties is a function, using the this keyword within that function will return the current object you are working under. This is useful if you'd like to retrieve the values of other properties within the same object.

For example:

// Javascript Code
var val = 'I never get seen.';

var o = {
    val: 'I do get seen!',
    fun: function() {
        // use `this` to reference the current object we are in!
        return this.val;
    }
};

// Outputs 'I do get seen!'
console.log(o.fun());

However, I can't figure out how to do the equivalent in PHP. I'm running PHP 5.6, so I have access to anonymous functions. Here is my sample code:

<?php
// PHP Code

$val = 'I never get seen.';

$o = array(
    'val' => 'I do get seen!',
    'fun' => function() {
        /**
         * I've tried:
         *
         * return $this.val;
         * return $this->val;
         * return $this['val'];
         * return this.val;
         * return this->val;
         * return this['val'];
         *
         * None of them work.
         */

        return $this->val;
    }
);

// Should output 'I do get seen!'
echo $o['fun']();

?>

EDIT:

As others are pointing out, I "should" be using a real class, and access the classes properties that way.

However, in the code I am working in, I do not have the luxury to making that paradigm change. If there are no alternative options, I'll keep in mind that there isn't an exact one-to-one equivalent for this idea within PHP.

romellem
  • 5,792
  • 1
  • 32
  • 64
  • Possible duplicate of [When to use self over $this?](http://stackoverflow.com/questions/151969/when-to-use-self-over-this) – André Dion Feb 10 '16 at 17:09
  • you should note that PHP actually hass the `class` keyword to define a class. not everything is an array. – Franz Gleichmann Feb 10 '16 at 17:09
  • I am not entirely sure if your approach is correct. In PHP, `$this` is used within the context of a class/object. – Maximus2012 Feb 10 '16 at 17:09
  • 1
    PHP and JS are different languages. Not everything is an object... – PeeHaa Feb 10 '16 at 17:10
  • In the code example that you posted, `echo $o['val'];` should give you what you are looking for. You may still get some other syntax error though. – Maximus2012 Feb 10 '16 at 17:11
  • @Maximus2012, it is entirely possible (and likely) my approach is wrong. I'm much more used to JS than PHP. Just trying to figure out how it would look in PHP to try and do something similar. – romellem Feb 10 '16 at 17:11

2 Answers2

3

How to define a class in PHP:

class test {
  private $val = "value";
  function fun() {
    return $this->val();
  }
}
//instantiation:
$obj = new test();
echo $obj->fun();

PHP is a language that actually supports classes and does not have to trick by using arrays for everything

Franz Gleichmann
  • 3,420
  • 4
  • 20
  • 30
0

It's important to note that you cannot just use all the things in one language (JS) and "convert" it to some other language (PHP). You could actually come close by using anonymous classes:

<?php

$o = new class {
    public $val = 'I do get seen!';

    public function fun() {
        // use `this` to reference the current object we are in!
        return $this->val;
    }
};

var_dump($o->fun());

Whether you should do this... I highly doubt it.

Different languages (especially prototype based and classical OOP based languages) just do things differently.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I know not all features of one language exist in others, I was asking if in this case, this feature _does_ exist. Thanks for the reply though, it is helpful. – romellem Feb 10 '16 at 17:22