Php array access to object

Интерфейс ArrayAccess

Интерфейс обеспечивает доступ к объектам как к массиву.

Обзор интерфейсов

Пример #1 Основы использования

class obj implements ArrayAccess private $container = array();

public function __construct () $this -> container = array(
«one» => 1 ,
«two» => 2 ,
«three» => 3 ,
);
>

public function offsetSet ( $offset , $value ) if ( is_null ( $offset )) $this -> container [] = $value ;
> else $this -> container [ $offset ] = $value ;
>
>

public function offsetExists ( $offset ) return isset( $this -> container [ $offset ]);
>

public function offsetUnset ( $offset ) unset( $this -> container [ $offset ]);
>

public function offsetGet ( $offset ) return isset( $this -> container [ $offset ]) ? $this -> container [ $offset ] : null ;
>
>

var_dump (isset( $obj [ «two» ]));
var_dump ( $obj [ «two» ]);
unset( $obj [ «two» ]);
var_dump (isset( $obj [ «two» ]));
$obj [ «two» ] = «A value» ;
var_dump ( $obj [ «two» ]);
$obj [] = ‘Append 1’ ;
$obj [] = ‘Append 2’ ;
$obj [] = ‘Append 3’ ;
print_r ( $obj );
?>

Результатом выполнения данного примера будет что-то подобное:

bool(true) int(2) bool(false) string(7) "A value" obj Object ( [container:obj:private] => Array ( [one] => 1 [three] => 3 [two] => A value [0] => Append 1 [1] => Append 2 [2] => Append 3 ) )

Источник

PHP ArrayAccess

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you’ll ever need. Get your copy today!

Programming Quickies

Quick dispatches from the life of a working programmer.

  • Laravel Objects
  • PHP ArrayAccess
  • Binding Objects as Laravel Services
  • PHP Magic Methods and Class Aliases
  • Unraveling Laravel Facades
  • Laravel Facade Troubleshooting
  • PHP Traits
  • Laravel’s MacroableTrait
  • Laravel Service Manager Indirection
  • Eloquent ORM Static Meta-Programming
Читайте также:  Php forms with validation

This week’s a quick remedial primer on PHP’s ArrayAccess interface. We’ll be back to Laravel next time, and promise you’ll see why we needed the primer.

First, some background and context. PHP has a native language type called an array. This isn’t a C array, where each element points to a specific memory location. Instead, a PHP array is an implementation of an ordered map. You can add any variable to an array. If you want to treat an array as an ordered list, you can add items with the following syntax

$array = []; $array[] = 'abc'; $array[] = 123; $array[2] = 'science'; 

You can also treat an array as a hash-map/dictionary

$array = []; $array['foo'] = 'bar'; $array['baz'] = 42; 

PHP also has a native language type called an object. To create an object, a user programmer must first define a class with the class keyword

and then use the new keyword to create an object from the class

PHP also includes a built in class called stdClass . A user programmer can use this class to create stand-alone objects without defining a class.

Finally, more modern versions of php include a shortcut for creating stdClass objects

Array vs. Object

Objects and arrays are different things in PHP, although they share some functionality. You can use an array as a dictionary like data structure

$array = []; $array['foo'] = 'bar'; $array['baz-bar'] = 'science'; echo $array['foo']; 

You can also use an object as a dictionary like data structure

$object = <>; $object->foo = 'bar'; $object-> = 'science' echo $object->foo; 

The only difference is the syntax. Arrays use the bracket characters [] , while objects use the “arrow” operator ( -> ). If you try to use array brackets to access the values on on object, PHP will issue a fatal error

Fatal error: Cannot use object of type stdClass as array …

So far, most of this is common knowledge to working PHP programmers. What’s less commonly known is PHP allows end-user-programmers to define what should happen when a programmer tries to access object properties with array syntax. Put another way, you can give your objects the ability to work with array brackets ( $o[] )

Читайте также:  Установить php curl centos

Array Access

PHP ships with a number of built in classes and interfaces. These classes and interfaces allow a PHP developer to add features to the language that would normally be reserved for a PHP core developer. The one we’re interested in today is ArrayAccess.

If you want to give your object the ability to act like an array, all you need to do is implement the built-in ArrayAccess interface. Give the following simple program a try

 $object = new Foo; $object['foo'] = 'bar'; echo $object['foo'],"\n"; 

In our class declaration statement we’ve included implements ArrayAccess . If we run this program, we’ll see the following error.

PHP Fatal error: Class Foo contains 4 abstract methods and must therefore be declared abstract or implement the remaining methods (ArrayAccess::offsetExists, ArrayAccess::offsetGet, ArrayAccess::offsetSet, …) in

Simply implementing an interface isn’t enough — we actually need to write the methods that are part of the interface synopsis. For a user defined interface, this would mean looking at the interface definition — for a built-in interface, we need to rely on the PHP manual. If you look at the manual, you’ll see the interface is defined as

That is, we need to define the 4 methods ArrayAccess::offsetExists, ArrayAccess::offsetGet, ArrayAccess::offsetSet, ArrayAccess::offsetUnset . Unfortunately, the help stops there. Interfaces are great for telling you what method you need to define, but less great at telling you what each method is supposed to do.

Let’s do a little detective work. Create the following short program, and then run it (either in your browser or via the command line)

 public function offsetGet ($offset) < echo __METHOD__, "\n"; >public function offsetSet ($offset, $value) < echo __METHOD__, "\n"; >public function offsetUnset ($offset) < echo __METHOD__, "\n"; >> $object = new Foo; $object['foo'] = 'bar'; 

This program defines the class Foo and implements each of the ArrayAccess interface’s abstract methods. Then, it instantiates a Foo object, and tries to set an array property. We’ve also programed each method to output its name whenever the program calls the method.

Читайте также:  Warning require once mail php

If you run the above program, you’ll see output something like this

Now try getting a property.

$object = new Foo; $object['foo'] = 'bar'; $bar = $object['foo']; echo "The value we fetched: " . $bar,"\n"; 

The above will output something like the following

Foo::offsetSet Foo::offsetGet The value we fetched: 

What did this teach us? When we tried to set a value via array access ( $object[‘foo’] = ‘bar’; ), behind the scenes PHP called our class’s offsetSet method. When we tried to get a value via array access ( $bar = $object[‘foo’]; ), behind the scenes PHP called offsetGet . However, no value was actually set or gotten from the object ( The value we fetched: ).

That’s how the PHP ArrayAccess interface works. It has some behind the scenes magic that will call a method on your class when you perform an array operation on your object, but it’s still up to you to implement that access. If you try something like this

_data); > public function offsetGet ($offset) < return $this->_data[$offset]; > public function offsetSet ($offset, $value) < $this->_data[$offset] = $value; > public function offsetUnset ($offset) < unset($this->_data[$offset]); > > $object = new Foo; $object['foo'] = 'bar'; $bar = $object['foo']; echo "The value we fetched: " . $bar,"\n"; 

You’ll have much better results.

While you initial reaction may be annoyance — “Why doesn’t PHP just implement this for me” — by giving you the responsibility of implementing your own ArrayAccess rules, the PHP core developers have given you (or the framework developers you rely on) tremendous power to create new behavior for your own objects.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 15th September 2014

Источник

Оцените статью