Where Do We Use the Object Operator "-≫" in PHP

Where do we use the object operator -> in PHP?

PHP has two object operators.

The first, ->, is used when you want to call a method on an instance or access an instance property.

The second, ::, is used when you want to call a static method, access a static variable, or call a parent class's version of a method within a child class.

Object operator in PHP (->)

In order to use the object operator, you will need to create and instantiate a class, as follows:

class MyClass {
public $myVar;

public function myMethod() {

}
}

$instance = new MyClass();

$instance->myVar = "Hello World"; // Assign "Hello World" to "myVar"
$instance->myMethod(); // Run "myMethod()"

Let me explain the above code:

  1. First, a class with a name of "MyClass" is created, with a variable of "myVar" and a method (basically a function within a class) with a name of "myMethod".
  2. "$instance" is created, and then it is assigned a new instance of the "MyClass" class.
  3. $instance->myVar, with the object operator accesses the public instance variable within the $instance object, and assigns it a value of "Hello World". Similarly, the "myMethod" is called within the $instance object, also using the object operator.

The object operator is simply PHPs way of accessing, running, or assigning "stuff" within an object.

Hope that helps.

PHP Accessing array values using Object Operator with key

this code work 100% fine

$arr = (object) array(
'id' => 123,
'title' => 'Example Title',
);

echo $arr->title;

PHP object Operator Precedence (->)

For a general idea of the precedence order between -> and [, you might want to take a look at the PHP7 migration documentation here.
I know you are talking about PHP5, but the migration documents pays attention to this since the behaviour was changed between PHP5 and PHP7.

To answer your question, $foo->$bar['baz'] is interpreted as $foo->{$bar['baz']} in PHP5. This means your code should be throwing an error because it is trying to access $b['f'] while it is using $b='b'; as the definition of $b.

However, in PHP7 it is interpreted as ($foo->$bar)['baz'], so here it should be working as you expect.

Are you sure your CLI isn't using PHP7?

PHP object operator building string within foreach?

You could store a reference of the object during the loop and assign your value at the end:

$final_object = new stdClass();

$array = ['one','two'];

$ref = $final_object ;
foreach ($array as $value) {
$ref->$value = new stdClass() ;
$ref = &$ref->$value ; // keep reference of last object
}
$ref = 999; // change last reference to your value

print_r($final_object);

Outputs:

stdClass Object
(
[one] => stdClass Object
(
[two] => 999
)
)

You could do the same using arrays:

$array = ['one','two'];
$final_object = [];
$ref =& $final_object;
foreach ($array as $value) {
$ref[$value] = [];
$ref =& $ref[$value];
}
$ref=999;
echo json_encode($final_object);

Outputs:

{"one":{"two":999}}

why would there be more than one object operator in a line of code in php?

It's called "method chaining". It allows you to apply more then one method, and thus do more then one thing, in one call. It's sort of the OOP equivalent of nesting functions.

can I use object operator -> in if statement

The answer is YES! If $images[1] contains an Object which has a link property and if that property is something that evaluates to true, the block will be executed.

class Test {
public $link = true;
}

$images = [new Test(), new Test()];

if ($images[1]->link) { /* will run */ }

If the $link property can be null, you also write:

if (!is_null($images[1]->link)) { … }

Using object Operator with mysql and php

You can fetch mysql_query result inform of array / object

For array you can use,
mysql_fetch_array();
http://www.php.net/manual/en/function.mysql-fetch-array.php

For objects you can use,
mysql_fetch_object();
http://www.php.net/manual/en/function.mysql-fetch-object.php

Also , mysql is depricated now from PHP. So you can use PDO / Mysqli

http://in1.php.net/manual/en/book.pdo.php



Related Topics



Leave a reply



Submit