📜 ⬆️ ⬇️

PHP and its embedded OOP trivia

I read an article about PHP and OOP called PHP and OOP magic back in that year, David Blaine described there in Kratz that there is OOP in PHP ... Yes, the author showed how to “properly” use functions correctly, but nowadays, Languages ​​have become not object-oriented but object-wise, you can’t be surprised by the simple support of OOP. Many PHP spread so that it is clumsy, slow ... Well, think, slow, think function parameters are taken in a different order =) it can be accelerated with accelerators, functions can be hidden in classes ... The most OOP in PHP is built-in interfaces ... That's about 2 of I will tell them (using the array as an example) ...

Interfaces are the tip of the iceberg called SPL, I learned about this thing right here, from the author of the riddle: make the code return a true - if ($ q [0] === 1 && $ q [0] === 2) (exactly I do not remember the task, I paraphrased it, so interesting)

To begin with the code, clarification later:
<?php

class MyArray implements ArrayAccess, Iterator
{
private $_data;
private $_innerCounter = -1;

public function __construct( $data = null )
{
$ this ->_data = $data;
$ this ->_innerCounter = count( $data );
}

public function get( $offset )
{
return $ this [$offset];
}

// start implementing ArrayAccess interface

public function offsetExists( $offset )
{
return isset( $ this ->_data[$offset] );
}

public function offsetGet( $offset )
{
if ( is_array( $ this ->_data[$offset] ) )
{
$this->_data[$offset] = new MyArray( $this->_data[$offset] );
return $this->_data[$offset];
}

return $ this ->_data[$offset];
}

public function offsetSet( $offset, $value )
{
$ this ->_data[$offset] = $value;
return $ this ;
}

public function offsetUnset( $offset )
{
unset( $ this ->_data[$offset] );
return $ this ;
}

// end implementing ArrayAccess interface

// start implementing Iterator interface

public function current()
{
if ( is_array( current( $ this ->_data ) ) )
{
return new MyArray( current( $ this ->_data ) );
}

//return $this->_data[$offset];
return current( $ this ->_data );
}

public function next()
{
$ this ->_innerCounter++;
return next( $ this ->_data );
}

public function key()
{
return key( $ this ->_data );
}

public function valid()
{
return $ this ->_innerCounter < count( $ this ->_data );
}

public function rewind()
{
$ this ->_innerCounter = 0;
//return reset( $this->_data );
return ;
}

// end implementing Iterator interface
}

$array = new MyArray( array(1, 2, 'qwe' => 'asd' , 6, 'obj' => array(1,2)) );

foreach ( $array as $arrayKey => $arrayValue )
{
if ( is_a( $arrayValue, 'MyArray' ) )
{
echo "it`s an MyArray Object with key '{$arrayKey}' -> " ;
var_dump( $arrayValue );
}
else
{
var_dump( 'key = ' . $arrayKey . ' value = ' . $arrayValue );
}
echo '<br>' ;
}

var_dump( $array[ 'qwe' ] );
echo '<br>' ;
var_dump( $array->get( 'qwe' ) );
echo '<br>' ;
var_dump( $array[ 'obj' ] );
echo '<br>' ;
var_dump( $array->get( 'obj' )->get(1) );

?>

// :
// string(17) "key = 0 value = 1"
// string(17) "key = 1 value = 2"
// string(21) "key = qwe value = asd"
// string(17) "key = 2 value = 6"
// it`s an MyArray Object with key 'obj' -> object(MyArray)#3 (2) { ["_data:private"]=> array(2) { [0]=> int(1) [1]=> int(2) } ["_innerCounter:private"]=> int(2) }
// string(3) "asd"
// string(3) "asd"
// object(MyArray)#2 (2) { ["_data:private"]=> array(2) { [0]=> int(1) [1]=> int(2) } ["_innerCounter:private"]=> int(2) }
// int(2)

* This source code was highlighted with Source Code Highlighter .


So ... There is a class MyArray that uses 2 interfaces: ArrayAccess and Iterator ... What are they doing? Let's start in a row.
')
ArrayAccess: an interface allowing access to your object as an array (access via square brackets, see comments in the code) example: $ array [1] or $ array ['qwe']
An indispensable tool in creating arrays!
:
ArrayAccess {
/* Methods */
// ( $offset - )
abstract public boolean ArrayAccess::offsetExists ( string $offset )
// ... $array['offset']
abstract public mixed ArrayAccess::offsetGet ( string $offset )
// ... $array['offset'] = 'value'
abstract public void ArrayAccess::offsetSet ( string $offset , string $value )
// ... :) unset $array['offset']
abstract public void ArrayAccess::offsetUnset ( string $offset )
}


Iterator: An interface that allows iteration through foreach / for (all objects have the ability to iterate, but there is control here;)) members of the object ... Naturally, which ones do you want (the default is only public properties)
Structure:
/* Methods */
// current( array(1,2,3) )
abstract public mixed Iterator::current ( void )
// key( array(1,2,3) )
abstract public scalar Iterator::key ( void )
// next( array(1,2,3) )
abstract public void Iterator::next ( void )
// rewind( array(1,2,3) )
abstract public void Iterator::rewind ( void )
// isset( $array[1]) - =)
abstract public boolean Iterator::valid ( void )


So what did we get? We got a base object with one “manual” get method, which returns an array element by key. The question is why does he need us if there is ArrayAccess? The answer is obvious (here I thought and checked the possibility of such an action: $ array ['qwe'] ['asd'] ... It works like this =) I'm happy!) Because you need to implement at least one method, at least for the sake of?!?!

In general, this is already the end, then any methods are not difficult to implement, the basic functionality is there, there is a full array, there is support for the cycles, there is control over the output data ... Add, change, give to your disposal ...

PS: PHP is an amazing language, with all its nemoshe, it’s done with it almost all, with all its unstructured, it can be fully structured under itself, with all its slowness, it can be accelerated to the speed of compiled languages ​​... I love this language he has no flaws, which is what you want, PHP-boxes I wish - do not hang your nose!
Zyy: if you were not here - you goof! =) Sorry for the expressions, but no manual will fill this site :)

UPD: at the request of workers clarification: this object will not behave as a full-fledged array due to the fact that arrays are simple data types, but reference objects, and in some situations this property manifests itself

Source: https://habr.com/ru/post/48697/


All Articles