📜 ⬆️ ⬇️

PHP and OOP magic

David: Magic OOP
Yeah, these guys, now I'll show you the magic of the PLO.

Instead of the preface. About the authors of the language



More precisely, about the grass that the developers smoked, how can you call functions? Well, take for example the functions for working with strings:

As we see it is very gloomy, and if we take arrays - in one place our function returns the result, in the other - it works by reference ... brr ...

Due to the lack of standards, even in the language itself, we have some discomfort when working with this language. Yes, and others are squinting ...
')

PHP is not OOP language



I often hear this tale. I decided to rewrite it :)

Decided and rewrote - on ruby a couple of classes that will be a wrapper for strings and arrays. These classes include only frequently used functions (in my humble opinion), and put the call to the rest on reflection.

Array - oArray



This class includes the following standard functions.



As well as some useful things:


For all functions of the array_ * type, I removed this prefix (after all, we already know what works with the array), the rest of the functions are on the conscience of the __call method and reflection.

String - oString



This class includes the following standard functions.



As well as some useful things:


For all functions of the str_ * and str * type, this prefix has been removed.

I also selected functions for working with URLs and stuffed them into a separate class (oUrl):


Slides


Now I will give an example of using this creation:
oArray ( ) // create an empty array
-> range ( 0 , 100 , 10 ) // wrapper for range
-> size ( ) // wrapper for sizeof
;


How do you? I still give food for the brain:
<? php

// create an array of elements
// => array ('module', 'controller', 'action')
oArray ( 'module' , 'controller' , 'action' )
// wrapper for array_combine
// => array ('module' => 'default', 'controller' => 'index', 'action' => 'index')
-> combine ( oArray ( 'default' , 'index' , 'index' ) )
// Wrapper for http_build_query returns oUrl
// => module = default & controller = index & action = index
-> to_url ( )
// add a piece of string to the beginning
// => http://domain.com/?module=default&controller=index&action=index
-> insert ( 'http://domain.com/?' )
// wrapper for parse_url returns oArray
-> parse ( )
// returns array ['host']
// => domain.com
-> host
;


And further:
oString ( "It's my way" )
// str_replace => "It's your way"
-> replace ( 'my' , 'your' )
// substr => "It's you"
-> sub ( 0 , 8 )
// str_pad => "It's you ..."
-> pad ( 11 , '.' )
// str_repeat => "It's you ... It's you ..."
-> repeat ( 2 )
// get oArray => array ('It's you', 'It's you', '')
-> explode ( '...' )
// remove empty elements
-> clear ( )
// array_merge => array ('It's you', 'It's you', 'Yes', 'No', 'Maybe')
-> merge ( array ( 'Yes' , 'No' ) , array ( 'Maybe' ) )
// implode => It's you; It's you; Yes; No; Maybe
-> implode ( ';' )
// return It's you; It's you; Yes; No; Maybe
-> get ( )
;


Object - oObject


Even an ordinary class can be improved by adding tricky heters / setters for the properties of an object (I checked it in RoR):

class MyClass extends Object_Object {
// this is a really protected property
protected $ _protected ;

// for these fields, you can define hetero / seters
protected $ title ;
protected $ description ;

// public property without heterov / setters
public $ data ;

/ **
* Setter for the title property
*
* @param string $ value
* @return mixed
* /
function setTitle ( $ value ) {
$ this -> title = ucfirst ( $ value ) ;
}

/ **
* Getter for title property
*
* @return mixed
* /
function getTitle ( ) {
return $ this -> title . '!!!' ;
}
}

$ MyClass = new MyClass ( ) ;
$ MyClass -> title = 'article title' ;

echo $ MyClass -> title ;


As a result of this fraud, we get the following result:

Article title !!!


As you can see - this class even has the right to exist;)

I got such a simple creation, you can download it from my blog:

oObject ver.0.1

PS These classes do not pretend to live in real projects, they are intended to dispel the myth of non-OOP efficiency of PHP, as well as serve as good material for learning by novice programmers ...

PPS I promised to write these things for the sake of holivar ...

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


All Articles