
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:
- str_replace (looking for the $ search string, replacing it with $ replace, in the $ subject [string [, total changed & $ count])
- strpos (in the $ haystack line, look for $ needle [, skip $ offset characters])
- substr_replace (in the $ string, replaced by $ replacement, from the position of $ start [, $ length])
- nl2br (change $ string string [this is $ is_xhtml])
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.
- array_diff - Calculate the difference in arrays
- array_fill - Fill an array with a specific value.
- array_filter - Applies a filter to an array using a callback function.
- array_flip - swap array values
- array_key_exists - Check if the specified key or index is present in the array.
- array_keys - Select all keys of an array
- array_map - Apply a callback function to all elements of the specified arrays
- array_pop - Extract the last element of an array
- array_product - Calculate the product of array values
- array_push - Add one or more elements to the end of the array
- array_rand - Select one or more random values ​​from an array.
- array_reverse - Returns an array with elements in reverse order.
- array_search — Searches for a given value in an array and returns the corresponding key if successful
- array_shift - Fetch the first element of an array
- array_slice - select array slice
- array_splice - Delete the sequence of array elements and replace it with another sequence
- array_sum - Calculate the sum of array values
- array_unique - Remove duplicate values ​​from an array
- array_unshift - Add one or more elements to the beginning of the array
- array_values ​​- select all array values
- sort - sort an array
- rsort - Sort an array in reverse order.
- natsort - Sort an array using a "natural order" algorithm
- natcasesort - Sort an array using a “natural order” algorithm without taking into account the case of characters
- ksort - Sort an array by keys
- krsort - Sort the array by keys in reverse order.
- implode - Combines the elements of an array into a string
As well as some useful things:
- to_url - http_build_query wrapper
- clear - Remove empty array elements
- odd - Leave only odd items
- even - Leave only even elements.
- size - Vraper for the sizeof function
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.
- addslashes - Escapes special characters in the string.
- count_chars - Returns information about characters in a string
- crc32 - Calculates CRC32 for a string
- crypt - Irreversible Encryption (Hashing)
- explode - Splits a string into substrings
- html_entity_decode - Converts HTML entities into matching characters
- htmlentities - Converts characters to the corresponding HTML entities.
- htmlspecialchars_decode - Converts special HTML entities back to the corresponding characters.
- htmlspecialchars - Converts special characters to HTML entities
- ltrim - Removes spaces from the beginning of a line.
- md5 - Returns the MD5 hash of a string
- money_format - Formats a number as a money value
- nl2br - Inserts the HTML line break code before each line break
- number_format - Formats a number with grouping
- parse_str - Parses a string into variables
- rtrim - Removes spaces from the end of a line
- sha1 - Returns the SHA1 hash string
- sprintf - Returns a formatted string.
- str_pad - Supplements the string with another string to the specified length
- str_repeat - Returns a duplicate string
- str_replace - Replaces the search string with the replacement string
- str_ireplace - Register-independent version of the str_replace () function.
- str_word_count - Returns information about words in a string
- strip_tags - Removes HTML and PHP tags from a string
- stripslashes - Removes character escaping from addslashes ()
- strstr - Find the first occurrence of a substring
- stristr - Register-independent version of the strstr () function.
- strpos - Returns the position of the first occurrence of a substring
- stripos - Returns the position of the first occurrence of a substring case-insensitive.
- strrpos - Returns the position of the last occurrence of a character.
- strripos - Returns the position of the last occurrence of a substring insensitive
- strrev - Flip a string
- strtolower - Converts a string to lower case
- strtoupper - Converts a string to upper case.
- substr_count - Returns the number of occurrences of a substring
- substr_replace - Replaces a portion of a string
- substr - Returns a substring
- trim - Removes spaces from the beginning and end of a line.
- ucfirst - Converts the first character of a string to upper case
- ucwords - Converts the first character of each word in a string to uppercase
- wordwrap - Performs a line break for a given number of characters using a line break character.
As well as some useful things:
- add - String Concatenation
- insert - Insert a string into a string
- size - vrper for strlen function
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):
- parse_url
- urlencode
- urldecode
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.1PS 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 ...