<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Harry Marx on Medium]]></title>
        <description><![CDATA[Stories by Harry Marx on Medium]]></description>
        <link>https://medium.com/@marxharry.hm?source=rss-ed5ec6b995b5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hQL3UX7tVKOh5BaaraAbiw.jpeg</url>
            <title>Stories by Harry Marx on Medium</title>
            <link>https://medium.com/@marxharry.hm?source=rss-ed5ec6b995b5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:37:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marxharry.hm/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Moina v3 (for PHP)]]></title>
            <link>https://medium.com/@marxharry.hm/moina-v3-for-php-2a181cdd69eb?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a181cdd69eb</guid>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Tue, 06 Oct 2026 05:19:29 GMT</pubDate>
            <atom:updated>2026-10-06T05:19:29.515Z</atom:updated>
            <content:encoded><![CDATA[<p>Moina is a syntax translator and functional enhancement for PHP.<br>* It simplifies some of PHP’s syntax<br>* It provides some additional language constructs<br>* It provides a few additional general purpose functions</p><p>Syntax simplifications/enhancements</p><p><strong><em>for()</em></strong> loops:<br>Consider simple <strong><em>for-</em></strong>loops where the counter is incimented only:<br><strong>for $i = 0 .. strlen($s)-1 { }<br></strong>The terminal condition will be calculated before the loop initiates…</p><p>switch statements:<br>Consider a simplified structure that includes implicit break statements:<br><strong>case <em>expression</em> {<br><em> value: statement ;<br>value, value : statement;<br>value: { many statements };<br>else: statement;<br>}</em></strong></p><p>records — simplified stdClass declarations:<br>Consider syntax used in JavaScript to define “inline objects”.<br>These are more appropiately named “records”:<br><strong>$student = { name: ‘harry’, hobby: ‘archery’, children:2 };</strong><br>and accessing “fields” on “records” with a dot-notation:<br><strong>echo $student.name;</strong></p><p>enum{}<br>Consider for declaring constants:<br><strong>enum{<br>cREGISTERED, cCANCELLED, cRETURNED,<br>cSENT=10,<br>cREJECTED = ‘Rejected’,<br>}</strong><br>where symbols without assigned values are allocated 1,2,4,8, etc.</p><p><strong>publish</strong> / <strong>published</strong>:<br>Consider declaring global variables, to be accessable globally, with no declarations inside functions.<br><strong>publish $my_name = ‘harry’;<br>function greet(){ echo ‘Hello’ . $my_name; }<br></strong>or listing many variables so:<br><strong>published $myname, $mydetails, $state;</strong></p><p>simplified arrays:<br>Consider <strong>[a:1,b:2]</strong> is now acceptable for <strong>[‘a’=&gt;1,’b’=&gt;2]</strong></p><p>typed function:<br>Consider <strong>int: function</strong> add( $i,$j) { return $i+$j; } to be equivalent to<br><strong>function add( $i, $j):int</strong> { return $i+$j;}</p><p>declarations:<br>Consider using the declarative operator ‘<strong>==&gt;</strong>’ instead of function definitions:<br><strong>myname ==&gt; ‘harry’; </strong><em>//will translate into a parameterless function</em><strong><br>add($i,$j) ==&gt; $i + $j; </strong><em>//standard untyped function<br></em><strong>gcd($i,$j)==&gt; { …….. return $out }</strong><em> //standard multi-line functions<br></em><strong>int: add( int $i,int $j) ==&gt; { return $i + $j; } </strong><em>//standard typed function</em><strong><br></strong>$array = map( <strong>($i) ==&gt; str($i)</strong> , $array ); <em>//callback/lambda/anon function</em><strong><br>$funcvar ??= (…$params) ==&gt; (object) $params; </strong><em>//function variable<br></em>(Function variables are quite useful for nested recursive functions, together with the null-coalencent assignment operator…)</p><p>global parameters for functions:<br>Consider a 2nd, implicit parameter defintion:<br><strong>greet($i)($me) </strong>==&gt; $i . ‘ ’ . $me;<br>being equivalent to <br><strong>function greet($i) { global $me; </strong>return $i . $me; }<br>It can then be used: greet(‘hello’); <br>where $me is an implicit, by ref, global parameter.</p><p>Language contructs:<br>Function <em>overriding, extendeing</em> and <em>overloading</em>.<br>All three constructs are global in effect — doing so on a function in the main page, will alter its behaviour, EVEN IN IMPORTED LIBRARIES!!!</p><p>overriding:<br>To replace an existing function with a new definition.<br><strong><em>override</em> div($a, $b) ==&gt; $a == 0 ? ‘undefined’ : $a / $b;</strong></p><p>extending:<br>To extend an existing function with more fucntionality handling additional cases, while making the existing function available for calling with current cases. The previously extended version is available through an appended underscore, but only within the extension:<br><strong><em>extend</em> div($a, $b) ==&gt; $a == 0 ? ‘undefined’ : div_($a,$b);<br></strong>The “extended” version is affected by the sequence in which the extentions are compiled.</p><p>overloading:<br>To write multiple versions of a single function, each with a uniqe signature.<br>A signature being the sequence, number and type of the values accepted as passed into the function. This provides runtime polymorhism to functions.<br><strong><em>overload </em>str( int $a ) ==&gt; ‘#’ . $a;<br><em>overload </em>str( string $a ) ==&gt; “ ‘{$a}’ ”;</strong></p><p>All three constructs are global in effect — overriding a function in the main page, will alter its behaviour, EVEN IN IMPORTED LIBRARIES!!!</p><p>General functions:</p><p>PHP allows <em>variadic</em> functions, which accept any number of arguments. When using named arguments with variadics, PHP packs these into an associative array. This is used often to declare a “struct” or “record”, which is an object like data structure, without methods. To switch between presenting a record as an associative array and an object, type casting can be used.<br>The functions <strong><em>obj()</em></strong> and <strong><em>arr()</em></strong> are also used internally by Moina to convert between these.<br><strong><em>struct()</em></strong> accepts a named parameter, used as a type name and then a record of such type: struct( circle: {radius: 10} ); <br>and return a record: { _type:circle, radius:10 }.<br>A record with the property _type, is parsed by <strong><em>valtype()</em></strong> to return ‘circle’ in this case. This is useful when functions are overriden:<br>override area( circle $a ) ==&gt; pi() * $a.radius ** 2;<br>as the internal override resolution uses valtype().<br>(Note that PHP does not define types for variables, but rather associate types with values inside the varianble.)</p><p>The native functions <strong><em>substr()</em></strong> and <strong><em>substr_replace()</em></strong> accepts start and length arguments. This is not always freindly. <strong><em>substring()</em></strong> and <strong><em>substring_replace()</em></strong> accepts start and end indexes to be extracted/replaced. Characters at both indexes are included in the action.<br><strong><em>strposrev()</em></strong> searches from a starting point in a string, towards the front of the string, and returns the index closest to this starting point if found, else <em>false.<br></em><strong><em>str_paired() </em></strong>will return the paired bracket (as found on $i), searching towards the end of the string.</p><p><strong><em>function split( $d, $s, $n = 0, $default=null )</em></strong><br><strong><em>split() </em></strong>and <strong><em>splitt() </em></strong>does the same as explode, but accepts an optional int $n to indicate the least and max number of elements the returning array should have. It fills missing elements with the default. <strong><em>splitt()</em></strong> also trims the elements, and drops empty elements, before appending nulls.<br><strong><em>splitp()</em></strong> does not drop empty elements. $d may be a regular expression, delimited with //;</p><p><strong><em>array_map_keys()</em></strong> and <strong><em>array_filter_keys()</em></strong> does the same as array_map and array_filter, but now the callback uses two parameters, for the key and the value.</p><p><strong><em>hide()</em></strong> and <strong><em>unhide()</em></strong> is used mostly to parse strings, and hide parts of the string by setting the 8th bit of the characters. Since PHP uses ascii in UTF-8 to store strings, the 8-bit is usually available for this simple sceme. <br> <br><strong><em>str()</em></strong> is an <em>overloaded</em> function, implying it converts its argument to a string, based on a typed implimentation of it. It is used by <strong><em>disp()</em></strong>, in conjuction with <strong><em>valtype()</em></strong>, to display custom types and formats. If you want to display or use the string representation of a new type, or to alter the way in which a type is converted to a string, you can override <strong><em>str()</em></strong> to do so.</p><p><strong><em>disp()</em></strong> and <strong><em>dispp() </em></strong>are variadic functions to generally display values. It uses <strong><em>valtype()</em></strong> and <strong><em>str()</em></strong> to convert any provided argument to a string representation. You can overload <strong><em>str()</em></strong> for each spcific value type.</p><p>formstr() is an alternative format to interpolate values into a string. It accepts variable names in {}-brackets, without the $. It allows for a format string: “{name} {name%9} {name%9.9} {name%x9.9}” where x can then be n/d/s/f. f as flag prepends sprintf() format definitions.</p><p><strong><em>push()</em></strong> and <strong><em>pop()</em></strong> are aliases for <strong><em>array_shift()</em></strong> and <strong><em>array_unshift()</em></strong>, treating an array as a stack, but using the first element as the stack’s top. <br><strong><em>aa_pop()</em></strong> retrieves a value from an associative array, and then unset() it from the array.</p><p><strong><em>any_of()</em></strong> and <strong><em>none_of()</em></strong> is wrappers for <strong><em>in_array()</em></strong>, with the advantage of using a function call, i.e. simpler syntax (named parameters) to define the array.</p><p>Use cases:</p><pre>&lt;pre&gt;<br>&lt;?php <br><br> //doubled parameters - 2nd grp is globals<br> $glb = &#39;harry&#39;;<br> dbl_long( $x )( $glb ) ==&gt; { return $x . $glb . &#39;&lt;br&gt;&#39;; };<br> echo dbl_long(&#39;hello &#39;);<br><br> dbl_short( $x )( $glb ) ==&gt; $x . $glb . &#39;&lt;br&gt;&#39;;<br> echo dbl_short(&#39;hello &#39;);<br><br> $dbl_long_var($x)($glb) ==&gt; { return  $x . $glb . &#39;&lt;br&gt;&#39;; };<br> echo $dbl_long_var(&#39;hello &#39;);<br><br> $dbl_short_var($x)($glb) ==&gt; $x . $glb . &#39;&lt;br&gt;&#39;; <br> echo $dbl_short_var(&#39;hello &#39;);<br> //--------------------------------<br><br><br> objdecl ==&gt; { name:&#39;harry&#39; };<br><br> $x = &#39;a&#39;;<br> case $x {<br>  &#39;a&#39;: $x = [1,2];<br>  &#39;b&#39;: $x = 2;<br>  else: $x = 3;<br> }<br> case( $x ) {<br>  &#39;a&#39;: $x = 1;<br>  &#39;b&#39;:  <br>  &#39;c&#39;: $x = 2;<br> }<br> case( $x ) {<br>  &#39;a&#39;,&#39;b&#39;,&#39;c&#39;:$x=1;<br>  &#39;c&#39;: $x = 2;<br> }<br><br><br> dx1 ==&gt; &#39;hallo1&#39;;<br> disp( dx1 );<br> dx2 ==&gt; { return &#39;hallo2&#39;; }<br> disp( dx2 );<br> dxxx ==&gt; {<br>  $dx3 ==&gt; &#39;hallo3&#39;;  <br>  disp( $dx3() );<br>  $dx4($name) ==&gt; &#39;hallo &#39; . $name;  <br>  disp( $dx4( &#39;harry&#39; ) );<br>  $dx5($name) ==&gt; { return &#39;hallo &#39; . $name; }; <br>  disp( $dx5( &#39;harry&#39; ) );<br> }<br> dxxx;<br><br> //making variables global without using &#39;global&#39;<br> publish $X; //or: published $X = &#39;gX&#39;;<br> $X = &#39;gX&#39;; <br> function x() { return $X . &#39; is accessible byref everywhere&#39;; }<br> echo x().&#39;&lt;br&gt;&#39;; <br><br> //declarations - functions in disguise<br> echo &#39;declarations look like expressions, but is functions: &#39; . Z .&#39;&lt;br&gt;&#39;;<br> Z ==&gt; &#39;ZZZ&#39;.&quot;&#39;s&quot;;<br><br> //a simplified for-loop syntax<br>  for $i = 1..10 {echo $i;} <br>  echo &#39;&lt;br&gt;&#39;;<br><br>  disp( { one:1, two:2 } ); //is obj if precede with (,=<br><br> //simplified function definitions: &#39;is given by&#39;<br> //also works for lamdas:  array_map( ($x) ==&gt; ...<br>  add($a, $b) ==&gt; $a + $b;  // fat-arrow functions <br>  add2($a, $b) ==&gt; { return $a + $b; } // fat-arrow function with {body}<br>  disp( add(1,2), add2(1,2) ); //disp() is a polymorphic echo<br><br> //extend and override<br>  //str() is an existing core function<br>  //this is an example of a sequence of extends, and calling previous versions<br>  xstr( $x ) ==&gt; print_r($x, true );<br>  extend xstr( $x1 ) ==&gt; &#39;v1 &#39; . xstr_($x1) ; //note the trailing underscore,<br>  extend xstr( $x2 ) ==&gt; &#39;v2 &#39; . xstr_($x2) ; //to chain to previous versions. <br>  extend xstr( $x3 ) ==&gt; &#39;v3 &#39; . xstr_($x3) ;<br>  echo xstr( 1 ) . &#39;&lt;br&gt;&#39;;<br>  //If you uncomment the next instruction, it nullify the above extensions,<br>  //and the output of the above echo changes accordingly<br>   override xstr( $x ) ==&gt; &#39;o &#39; . xstr_($x);<br>  //You cannot use the fn_() form to access an extended version in an overridden version.<br>  //You cannot use a particular version, override it, and then use the overridden version.<br>  //Overriding or extending a function does so globally !!!<br><br> //Now we demo overload, where the exact version of the namesake function is based on the type of arguments<br> //This form is polymorphism is runtime, and based on the type of the value passed, <br> //not the type associated with the variable!<br> //The function valtype() is used by this mechanism to route the call to the correct overload.<br> <br> disp(&#39;&lt;hr&gt;&#39;);<br> //More complex demo:<br> //Define a function display(), that uses an overloaded function val() to format the output.<br> //The overload resolution uses valtype(), which can also be extended.<br> //This will call the relevant polymorhic function val()<br> function display( ...$x ) {<br>  foreach ( $x as $i =&gt; $xx ) {<br>   if ( $i ) echo &#39; &#39;;<br>   echo val( $xx );<br>  }<br>  echo &#39;&lt;br&gt;&#39;;<br> }<br> //Here we defined independant overloaded versions of val() that will be called based<br> //on the type returned from valtype(), for the actual value passed.<br> val( $i ) ==&gt; print_r( $i, true );    // this is a root, non-overloaded default<br> overload val( integer $i ) ==&gt; &#39;ival=&#39; . $i; // should be called for integers<br> overload val( string $s ) ==&gt; &#39;sval=&#39; . $s;  // should be called for strings <br> <br> //extend valtype() to return some custom type based on some arbitrary condition<br> //valtype() can only be extended, never overloaded!!!<br> //this is because valtype is used to resolve overloads, which will loop...<br> extend valtype( $v ) ==&gt; $v === 1 ? &#39;number1&#39; : valtype_($v);<br> // returns &quot;number1 integer string&quot;<br> display( valtype(1), valtype(2), valtype( &#39;xxx&#39; ) ); <br> //define an overload formatter now for exactly this new custom type<br> overload val( number $i ) ==&gt; &#39;#&#39; . $i;   <br> <br> display( 1, 2, &#39;x&#39; );<br> //disp( 1, 2, &#39;x&#39; );<br> // The output: &#39;#1 ival=2 sval=x&#39;<br> // valtype() returned &#39;number&#39; for &#39;1&#39;, and this caused the overload resolution to call <br> // val( number $i ), when display() tried to display the 1.<br><br> //Note - valtype() will, if the object has a property &#39;valtype&#39;, return its value<br> //This allows for &quot;typed objects&quot;<br> $to = {_type:&#39;developer&#39;, name:&#39;harry&#39; };<br><br> //The dot-operator has now two functions - getting an object&#39;s member, or concatenating strings.<br> //If any of the two chars next to the dot is a not alphabetic, it is seen as concatenation.<br> //If both are alphabetic, it is used as a member selection.<br> disp( $to.name . &#39; is a &#39; . valtype( $to ) );<br><br> display( [1,2,3] );<br> //If the below definition is not added, and we did not define a &quot;root&quot; function with no overloading,<br> //you will get: Exception: overload signature not found: val(list)<br> //This indicates that display() could not find an appropiate val() with which to display it.<br> //You can resolve this by either defining the root function, which will handle all uncatered types,<br> //or by defining an overload:<br> overload val( list $x ) ==&gt; &#39;list=[&#39;.join(&#39;, &#39;, $x) . &#39;]&#39;; <br><br> //complex signatures - multiple parameters<br> //the pattern of arguments forms the &quot;signature&quot; of the overload to call.<br> //if not found, it will drop the last type, and search again, until a root overload.<br> //if none found, it will fail and throw an exception.<br> //This we call degenerative overloading.<br> overload function do_x( integer $i ) { disp( &quot;do1int: $i&quot; ); }<br> overload function do_x( integer $i, integer $j ) { disp( &quot;do2int: $i $j&quot; ); }<br> overload function do_x( integer $i, integer $j, integer $k ) { disp( &quot;do3int: $i $j $k&quot; ); }<br> overload function do_x( string ...$s ) { disp( &quot;do strings:&quot;, ...$s ); } <br> do_x(2);<br> do_x(2,3);<br> do_x(2,3,4);<br> do_x(2,3,4,5); //signature will degerate until the first type match is found<br> do_x( &#39;a&#39;,&#39;b&#39;,&#39;c&#39; ); //this signature will match degenaratively <br> //Only the last parameter of overload functions may be untyped<br> //You cannot use &#39;mixed&#39; to match any type<br> //Parametes may have default values - useful if matched degeneratively.<br> //Optional parameters not applicable to overloaded functions<br><br>//overriding an existing overload, will override ALL overloads.<br>//override function do_x( string ...$s ) { disp( &quot;this replaces all overloads:&quot;, ...$s ); } <br><br>//extending an existing overload, will override ALL overloads......... :-(<br>//I would like this not to ... but don&#39;t have an elegant solution yet..............<br><br> //Degeneracy approaches inheritance. If the arguments&#39; signature does not match, <br> //overloading tries a generative signature - take note of the type expressions and <br> //operators (in priority sequence). <br> overload area(     shape $s ) ==&gt; &#39;abstract&#39;;<br> overload area(    circle $s ) ==&gt; &#39;circle area is:&#39; . pi() * $s.radius ** 2;<br> overload area( rectangle $s ) ==&gt; &#39;rectangle area is:&#39; . $s.width * $s.height;<br> overload area(    square $s ) ==&gt; &#39;square area is:&#39;. $s.length ** 2;<br><br> disp( &#39;circle:&#39;, area( struct( circle: { radius:10 } ) ) );<br> disp( &#39;rectangle:&#39;, area( struct( rectangle:{ width:10, height:10 } ) ) );<br> disp( &#39;square:&#39;, area( struct( square: { length:10 } ) ) ); <br> //the following call will use a &#39;shape_square&#39; overload, else degenerate to a &#39;shape&#39; overload<br> disp( &#39;shape_square:&#39;, area( struct( shape_square: { length:10 } ) ) ); <br> //the following call will use square or shape, to find an overload<br> disp( &#39;square|shape:&#39;, area( { _type:&#39;square|shape&#39;, length:10 } ) ); //here the _type syntax must be used<br> //the following call will use a &#39;square&#39; overload, then &#39;shape&#39;<br> disp( &#39;shape&gt;square:&#39;, area( { _type:&#39;shape&gt;square&#39;, length:10 } ) );<br> //the following call will use a &#39;square&#39; overload, then &#39;shape&#39;<br> disp( &#39;square,shape:&#39;, area( { _type:&#39;square,shape&#39;, length:10 } ) ); <br><br> //You can also use type expressions when overloading a function: <br> overload do_x( staff|client ...$s ) ==&gt; &quot;do staff/client: &quot; . str_struct( ...$s ); <br> disp( do_x( struct(  staff: {name:&#39;harry&#39;} ) ) );<br> disp( do_x( struct( client: {name:&#39;harry&#39;} ) ) );<br><br> disp( splitt( &#39;,&#39;, overload_type_expression( &#39;a|b_1|2&#39; ) ) );<br><br><br> //There are alternative mechanisms in native PHP, which allows polymorhism inside a function.<br> //The first uses getType() on an untyped parameter, which will return the type of the value passed  <br> //( which is the basis for valtype() ).<br> function do_show1( $x ) {<br>  if ( getType($x) === &#39;integer&#39; ) echo &#39;interger:&#39; . $x . &#39;&lt;br&gt;&#39;;<br>  if ( getType($x) === &#39;string&#39; ) echo &#39;string:&#39; . $x . &#39;&lt;br&gt;&#39;;<br> }<br> do_show1( 1 );<br> do_show1( &#39;x&#39; );<br> //This approach limits the polymorhism to native PHP types, and does not allow new types to be processed, <br> //without modifying the existing function. In contrast, OOX does allow new forms of the function, without<br> //modifying existing code.<br> <br> //The second alternative is to use named parameters with a variadic functions.<br> //The passed values are packaged in an associative array.<br> function do_show2( ...$x ) {<br>  if ( isset( $x[&#39;int&#39;] ) ) echo &#39;int:&#39; . $x[&#39;int&#39;] . &#39;&lt;br&gt;&#39;;<br>  if ( isset( $x[&#39;str&#39;] ) ) echo &#39;str:&#39; . $x[&#39;str&#39;] . &#39;&lt;br&gt;&#39;;<br> }<br> do_show2( int:1 );<br> do_show2( str:&#39;x&#39; );<br> //This approach is not limited to native or even define types, but totally dynamic.<br> //It does require the arguments be named correctly though.<br> //It also does not isolate previously written code like an overload approach does.<br><br> //you can overload base functions<br> overload str( $x ) ==&gt; { // overload<br>  if ($x === null) return &#39;null&#39;;<br>  if ($x === true) return &#39;true&#39;;<br>  if ($x === false) return &#39;false&#39;;<br>  if ($x === 0) return &#39;zero&#39;;<br>  return print_r( $x, true ); <br>  }<br>  disp(&#39;overoading base function:&#39;, 0);<br><br>//--------<br><br><br> //You can change the type of a struct by simply calling struct() again, or by setting _type. <br> $s = struct( { name: &#39;harry&#39; } ); //untyped - disp() as stdClass<br> disp($s);<br> $s = struct( boss: $s ); //can be re-typed<br> $s._type = &#39;staff&gt;boss&#39;; //can be re-typed with type-expression<br> disp( $s );<br> overload str( boss $s ) ==&gt; &#39;His Great &#39; . strtoupper($s.name);<br> overload str( staff $s ) ==&gt; &#39;Hardworking Staff &#39; . strtoupper($s.name);<br><br><br>//--------<br>disp(&#39;&lt;hr&gt;Use declaratives as destination in assignment statements:&lt;br&gt;&#39;);<br><br>disp(&#39;This declaration is written to accept 1 or 2 arguments.&#39;);<br>disp(&#39;When presented 1, it acts like a getter();&#39;);<br>disp(&#39;When presented 2, it acts like a setter();&#39;);<br>disp(&#39;Moina allows an alternative setter syntax: called with 1, yet assigned a value:&#39;);<br><br>published $_instance = rand() . rand() . rand();<br>instance( $k = null, $v = null ) ==&gt; { <br> if ( $k !== null &amp;&amp; $v !== null ) { //setter<br>  if ( str_ends_with( $k, &#39;{}&#39; ) ) foreach ( $v as $kk =&gt; $vv ) {<br>   $_SESSION[ $_instance ][ substr($k,0,-2) ] ??= {};<br>   $_SESSION[ $_instance ][ substr($k,0,-2) ]-&gt;$kk = $vv;<br>  }<br>  elseif ( str_ends_with( $k, &#39;[]&#39; ) ) foreach ( $v as $kk =&gt; $vv )<br>   $_SESSION[ $_instance ][ substr($k,0,-2) ][$kk] = $vv;<br>  else<br>   $_SESSION[ $_instance ][ $k ] = $v;<br> }<br> if ( $k === null ) return $_SESSION[ $_instance ]; //getter all<br> return $_SESSION[ $_instance ][$k] ?? null; // getter<br>}<br><br>instance(&#39;name&#39;) = &#39;harry&#39;;<br>disp( &quot;1. &quot;, instance( &#39;name&#39; ) );<br><br>instance(&#39;arr_test&#39;) = [&#39;i1&#39; =&gt; &#39;v1&#39; ];<br>disp( &quot;2. &quot;, instance() );<br><br>instance(&#39;arr_test[]&#39;) = [&#39;i2&#39; =&gt; &#39;v2&#39;, &#39;i3&#39; =&gt; &#39;v3&#39; ];<br>disp( &quot;3. &quot;, instance(), instance(&#39;arr_test&#39;)[&#39;i2&#39;] );<br><br>instance(&#39;obj_test{}&#39;) = { p1: &#39;v1&#39; };<br>disp( &quot;4. &quot;, instance() );<br><br>instance(&#39;obj_test{}&#39;) = {p2: &#39;v2&#39;};<br>disp( &quot;5. &quot;, instance(), instance(&#39;obj_test&#39;)-&gt;p2 );<br><br>instance(&#39;obj_test&#39;) = {p3: &#39;v3&#39;};<br>disp( &quot;5. &quot;, instance(), instance(&#39;obj_test&#39;)-&gt;p3 );<br><br>//---------------------------<br>disp(&#39;&lt;hr&gt;Use double parameter definition for passed, by reference(!), and recursive parameters&#39;);<br>disp( &#39;(Declaratives as parameters causes delayed evaluation. Only when it is used in called function,<br>is it evaluated.)&#39; );<br><br>$one = 1;<br>$add = ( ...$p )( $add, $one ) ==&gt; {<br>   if ( count($p) == 0 ) return 0;<br>   if ( count($p) == 1 ) return $p[0];<br>   $p0 = pop( $p );<br>   return $one + $p0 + $add( ...$p ) ;<br>  };<br>  disp( $add(1,2,3,4,5), &#39; == 19&#39; );<br><br>?&gt;</pre><p><a href="https://github.com/marxharryhm-hub/PHP-overloading">https://github.com/marxharryhm-hub/PHP-overloading</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a181cdd69eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PHP is not perfect]]></title>
            <link>https://medium.com/@marxharry.hm/php-is-not-perfect-bc4a00632a00?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc4a00632a00</guid>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Wed, 20 May 2026 11:59:26 GMT</pubDate>
            <atom:updated>2026-07-11T05:57:18.382Z</atom:updated>
            <content:encoded><![CDATA[<p>Well about its syntax at least…</p><p>For example, whenever you make a rule, make a way to break it, and you will control how this happens. If not, anything goes.</p><p>Think about globals. All variables inside functions are local, and all in the main body is local to the main body. You have to declare them inside the function as ‘global’, before you can access it in the function, or you can use $GLOBALS[]. Which is not bad, but it does not always resolve in the most elegant solutions. Sometimes, I want to have a global variable, and am using it everywhere. So what about this idea:</p><pre>published $X = &#39;gX&#39;; // replace all occurances of $X with $GLOBAL[&#39;X&#39;]<br>function x() { return $X . &#39; is accessible byref everywhere&#39;; }<br>echo x();</pre><p>But how can we do this? With a simple pre-processor. It should read the source code, find /[\n\r]\s*published\s/, scrape the listed variable names, and do the replace. And then we can hook this pre-processor onto auto_prepend_file in the php.ini.</p><p>(Take note that you can do “global” values using static properties in a static class too… but it can become clunky quickly. )</p><p>The moment you do this pre-processor, you immediately try some other syntax enhancement. For example:</p><pre>// instead of<br>// for ($i=0; $i&lt;count($a); $i++ ) { ... }<br>// lets use:<br>  for $i=0 .. count($a)-1 { ... }<br>// which translates to:<br>  $_i_ = count($a)-1; for($i=0; $i&lt;=$_i_; $i++ ) { ... }</pre><p>This takes care of two problems — the complex syntax for simple for-loops, and the evaluation of count($a), which now happens only once instead of for each iteration. It also allows for something like: for $p=$p..$p+1 { … } which normally would have looped forever.</p><p>Here is another nice idea. Instead of doing functions using “function”, and lambdas using fn(), why not:</p><pre>myfunction(...$params) ==&gt; expression; //OR<br>myfunction(...$params) ==&gt; { return expression; } //OR<br>usort($A, ($a,$b) ==&gt; $a-&gt;val &lt;=&gt; $b-&gt;val );</pre><p>And on the topic of globals — functions are of course global. What if we allowed an even simpler syntax for global unmutable values, parading a symbol as a function without parameters?<br><strong>X ==&gt; ‘harry’<br></strong>…just a thought.</p><p>You can also use a declerative as “getters” and “setters”… Assume you write a declaration with two parameters, a key and a value pair. <br><strong>staff( $persno, $name=null ) ==&gt;</strong> …snipped.<br>The implementation then should set the staff’s name if provided, and return the staff’s name if not. Then you can use it as follows:<br><strong>staff( 123 ) = ‘harry’;</strong><br><strong>echo staff(123);</strong></p><p>And on the topic of variadics and objects… you can combine a variadic parameter with named arguments: something like:<br><strong> obj(…$x) ==&gt; (object) $x;</strong><br>and then use it like:<br><strong> $x = obj( name: ‘harry’, surname: ‘marx’ );</strong><br>Or even better, translate {}=brackets whenever used in expressions as obj():<br><strong>$x = { name: ‘harry’ }; </strong>// translating to $x = obj( name: ‘harry’ )<br>And we can even throw in a replacement for the -&gt; property selector, a dot.<br>Yes, if the dot matches /(\$[\w._]+)\.(\w)/ then replace it with -&gt;<br>(I love it how we are bringing the client side javascript and severside PHP together. Also think of substring() and substring_replace(). )</p><p>Or a simple:</p><pre>enum {A,B,C,D}</pre><p>which defines A=1, B=2, C=4, D=8</p><p>But here is a much bigger idea — how about function overloading? And here I’m not thinking about OOP and how PHP does it. Rather, the real deal, like in runtime polymorphic functions. I’m thinking of the same function name, on different definitions, using different parameter types. And again, even further, non-OOP user defined parameter types.<br>Consider:</p><pre> overload area(    shape $s) ==&gt; &#39;abstract&#39;;<br> overload area(   circle $s) ==&gt; &#39;circle area is:&#39; . pi() * $s.radius ** 2;<br> overload area(rectangle $s) ==&gt; &#39;rectangle area is:&#39; . $s.width * $s.height;<br> overload area(   square $s) ==&gt; &#39;square area is:&#39;. $s.length ** 2;</pre><p>Note, we never defined what these types are, yet</p><pre>disp( &#39;circle:&#39;,     area( struct( circle:    { radius:10 } ) ) );<br>disp( &#39;rectangle:&#39;,  area( struct( rectangle: { width:10, height:10 } ) ) );<br>disp( &#39;square:&#39;,     area( struct( square:    { length:10 } ) ) );</pre><p>all works. But what is this struct() function? (Again the magic of a variadic!)</p><pre>function struct( ...$T ) { <br>  $t = array_key_first($T); $T[$t]-&gt;_type = $t ?: null; return $T[$t]; <br>}</pre><p>It builds a base or “standard” object, with a property ‘_type’ which it sets to the name of the parameter being passed.</p><p>Now to parse the overloads is a bit tricky — we rename each overloaded definition with a unique function name, store these against the types they accept in an associative array, and replace all calls to the overloaded function with a “router”. The router takes as first parameter the actual overloaded function name, then the rest of the parameters. It then selects the relevant function to call, matching the types of the passed arguments with the required parameters.</p><p>One last detail for the overloading — we define a new function valtype(), which recognize our struct() types, the standard types and class names, and a bit more. All of this, we build into the pre-processor.</p><p>Now you cannot redefine a function in PHP. You can define a function variable: $func = ($params) ==&gt; ‘do something’;<br>And then later redefine it, as there is no restriction on redefining a variable. but this then runs into the <em>publish</em> restriction we started with, and the timing of doing this.<br>Another solution is simply:<br><em>override</em> func($params) ==&gt; ‘do something more’;<br>The pre-processor can now do exactly the same it did for overload, but just skip the type matching, and always calls the latest version.<br>As the pre-processor acts like a pre-compiler, functions defined anywhere like this, becomes relevant everywhere.</p><p>What is also nice about using such a pre-processor, is you can define functions in it, for use in all your projects. For example disp(), which can display any type. And using <em>overload str()</em>, you can even define the function it should use to format a specific type. Also formstr() and scan() are two powerful ideas.</p><p>As with everything powerful, you can always shoot yourself in the foot. A big no-no would be to <em>override</em> an existing PHP function — make the life of the maintenance crew impossible, redefine strpos() to be based 1. At least you would have a short laugh. Yet there is a lot of scope for <em>overload. </em>We could <em>overload</em> <em>strpos()</em> to accept regular expressions as needle… which would be all ok. The sky is the limit…</p><p>So no, perhaps PHP does not conform to what you would consider to be a perfect programming syntax, but then, nothing is stopping you from doing the unthinkable…</p><p>The pudding …<a href="https://github.com/marxharryhm-hub/PHP-overloading.git">https://github.com/marxharryhm-hub/PHP-overloading.git</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc4a00632a00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Extending PHP]]></title>
            <link>https://medium.com/@marxharry.hm/extending-php-ea428fd58bb2?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea428fd58bb2</guid>
            <category><![CDATA[overloading]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[syntax]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Tue, 14 Apr 2026 13:04:06 GMT</pubDate>
            <atom:updated>2026-07-11T06:40:29.177Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Having worked in many languages, I am partial to enhancing PHP syntax a bit…<br>For this I hooked into the <em>auto_prepend_file</em> setting in the php.ini, and made some syntax “enhancements”. I’m calling it <em>Moina — </em>as in a small member of the water fleas species.</p><p>Encapsulation, inheritance and polymorphism was the main drive force for building OOP. Yet OOP is not the only paradigm with dominion over these concepts. I would like to explore how some of these technical concepts in OO, can be realized in non-OO domains. The reason being the overhead in many OO languages, and syntactical verbosity.</p><p>Encapsulation is the collecting of variables (properties) into a structure (a class). Polymorphism is mapping functions (methods) to these structures. And then inheritance is the mechanism to progress from abstract classes to specialized implementations thereof.</p><p>I will discuss PHP without referring to its OO implementation.<br>I have developed a parser to run on PHP files before the official engine takes over, and it brings a few ideas to the front.</p><h4>Encapsulation</h4><p>‘Moina’, the parser, promotes the easy grouping of fields into <em>structures,</em> with the minimum of syntax, using a variadic function as intermediate. <br>Consider the syntax for associative arrays: <br><strong> $A = [‘name’ =&gt; ‘harry’, ‘job’ =&gt; ‘developer’ ];</strong><br>and accessing it:<br> <strong>echo $A[‘name’] . ‘ is a ’. $A[‘job’];<br></strong>Could this be less verbose?</p><p>PHP does allow the creation of an object, based on associative arrays:<br> <strong>$O = (object) [‘name’ =&gt; ‘harry’, ‘job’ =&gt; ‘developer’ ];</strong><br> <strong>echo $O-&gt;name . ‘ is a ‘ . $O-&gt;job;<br></strong>This is slightly less syntactical.</p><p>Such an object instantiates stdClass, and as such does not have methods. For our purposes we can call this a <em>Structure</em>, borrowing from C/C++. But PHP also allows named parameters when calling functions, and variadic functions which accepting any number of arguments. When calling a variadic, with a named parameter, you can actually use any name for the argument. Combining these concepts into a type casting function, like in:<br><strong> function obj( …$x ) { return (object) $x; }<br></strong>simplifies the declaration too:<br><strong> $O = obj( name: ‘harry’, job: ‘developer’ );</strong><br><strong>echo $O-&gt;name . ‘ is a ‘ . $O-&gt;job;</strong></p><p>And then we can have the parser taking typical javascript syntax, and rewriting it in the above format, from:<br> <strong>$O = { name: ‘harry’, job: ‘developer’ }; <br> echo $O.name . ‘ is a ’ . $O.job;<br></strong>(The parser sees /(\$[\w._]+)\.(\w)/ as the object operator.)</p><p>Is this syntactical simplification clear? Does it contributes to readability, <br>and less cross language barriers?</p><h4>Polymorphism</h4><p>This allows multiple definitions using a single function name, to be mapped each to a particular structure. In OO this usually is done through abstract classes and (inheritance) extended implementations.<br>But another concept is fairly well defined — function overloading. NOT the PHP definition, but the “standard” definitions of “overloading”. This is where the mapping is done based on the type of the values the function is called with, and the types of values the function accepts. We will call this the arguments’ signature, and the function’s signature.<br>In OO the namespace problem is approached from the variable’s perspective:<br><strong>$circle-&gt;draw();</strong><br>where circle is some object, an instantiation of a Circle class, which is derived from perhaps an abstract Shape class.<br>Typically the Shape class presents an abstract draw() method.</p><p>In a non-OO domain, a draw() function would be defined, which accepts as argument, a particular shape:<br><strong> draw( $circle );</strong><br>(For me, this instruction flows more naturally with the verb before the noun.)<br>Moina allows for the redeclaration of the function draw() (using the ‘overload’ keyword), where each version would accept a different pattern of argument types — each has a different <em>signature</em>.<br><strong>o<em>verload </em>function draw( circle $S ) { }<br><em>overload</em> function draw( square $S ) { }<br></strong> …etc.<br>Note important differences here. First, the selection of the function to be executed, the overload resolution, happens NOT on the type of the <em>variable</em>, but on the type of the <em>value</em> in the variable. This takes advantage of the “untyped” nature of PHP. All values in PHP have a type, but the type is associated with the value, and not the variable (the container), in which the value is stored. And then the overload resolution happens at runtime, not compile time.</p><p>I could define structures now as follows:<br> <strong>$circle = { _type: ‘circle’, x:100, y:100, radius:30 };<br></strong>Setting the property <em>_type</em> causes the function <em>valtype()</em> to return ‘circle’ when used to resolve overloading, and so we have <em>typed structures</em>.<br>I can also use the <em>struct()</em> function:<br> <strong>$circle = struct( circle: { x:100, y:100, radius:30 } );<br></strong>(Much more elegant…)</p><p>And a function to get its area:<br><strong><em>overload</em> function area( circle $c ) { return $c-&gt;pi() * radius ** 2; }<br></strong>or even using Moina’s double-fat-arrow declarations:<br><strong><em>overload </em>area( circle $c ) ==&gt; pi() * $c.radius ** 2;<br></strong>(note the dot operator doubling for the object operator)</p><p>More examples and other ideas</p><p>An example of a simplified for-loop syntax, for simple loops:<br><strong>for $i=1..10 { echo $i; }</strong></p><p>Declaring a <em>structure</em><br><strong>$s = { one:1, two:2 };</strong> // curlies preceded with the characters ( , : = becomes objects<br><strong>$s = struct( person: {name: ‘harry’ } );</strong> // a typed structure</p><p>Simplified function definitions: double-fat-arrows: <br><strong>add($a, $b) ==&gt; $a + $b; <br>add2($a, $b) ==&gt; { return $a + $b; }</strong> // fat-arrow function with {body}<br>and as a callback:<br><strong>preg_replace_callback( ‘[a-z]’, ($match) ==&gt; strtoupper($match[0], $a );<br></strong>(note here the superfluous and now missing ‘fn’)</p><p>(You can also use ==&gt; for “functions” without parameters. <br><strong>myName ==&gt; ‘harry’; <br></strong>Conceptually these becomes global constants, that accepts expressions…)</p><p>Another way of using declaratives is as both “getters” and setters”.<br>If you declared:<br><strong>staff_name( $number, $name=null ) ==&gt; { global( $STAFF);<br> if ($name) $STAFF[$number] = $name;<br> return $STAFF[$number] = $name; }<br></strong>you can use it in an “assign” statement where the value takes the place of the last parameters:<br><strong>staff_name( 123 ) = ‘harry’;</strong></p><p><em>extend</em> and <em>override </em>allows even more OO concepts to spill into non-OO PHP. Assume <em>str()</em> is an existing function. This is an example of a sequence of <em>extends</em>, and calling previously extended versions:<br><strong>str( $x ) ==&gt; print_r($x, true ); </strong>// the “root” form<strong><br>extend str( $x1 ) ==&gt; ‘v1:’ . str_($x1); </strong>//note the trailing underscore to call<strong><br>extend str( $x2 ) ==&gt; ‘v2:’ . str_( $x2 ); </strong>//to chain to previous versions.<strong><br>extend str( $x3 ) ==&gt; ‘v3:’ . str_( $x3 ); <br>echo str( 1 );</strong>// output : v3 v2 v1 1</p><p>If you uncomment the next instruction, it nullifies the above extensions,<br>and the output of the above echo changes accordingly<br><strong>//override str( $x ) ==&gt; $x;<br></strong>Overriding or extending a function does so globally !!!</p><p>However, to extend an overloaded function, simply overload the function again, with the same type signature. Moina will replace the previous overload with the new one.</p><p>More complex overloading:<br>Define a new function <em>display()</em>, that uses an overloaded function <em>val()</em> to format the output. The overload resolution uses the Moina function <em>valtype()</em>, which can be extended.</p><pre>function display( …$x ) {<br>  foreach ( $x as $i =&gt; $xx ) {<br>    if ( $i ) echo &#39; &#39;;<br>    echo val( $xx );<br>  }<br>  echo &#39;&lt;br&gt;&#39;;<br>}<br><br>val( $i ) ==&gt; print_r( $i, true ); // base function, non-overloaded default<br>overload val( integer $i ) ==&gt; &#39;ival=&#39; . $i; // should be called for integers<br>overload val( string $s ) ==&gt; &#39;sval=&#39; . $s; // should be called for strings</pre><p>Here we defined independent overloaded versions of val() that will be called based on the type returned from <em>valtype()</em>, for the actual value passed. <br>We can extend <em>valtype()</em> to return some custom type based on some arbitrary condition:<br><strong> extend valtype( $v ) ==&gt; $v === 1 ? ‘number’ : valtype_($v);<br></strong>Calling:<br><strong>display( valtype(1), valtype(2), valtype( ‘xxx’ ) ); </strong><br>returns: number integer string<br>And lastly, we can define an overload formatter now for exactly this new custom type:<br><strong> overload val( number $i ) ==&gt; ‘#’ . $i; </strong><br><strong>display( 1, 2, ‘x’ );</strong><br>returns: #1 ival=2 sval=x</p><p>Note — <em>valtype()</em> will, if an object has a property ‘_type’, return its value as the object’s type.<br>This allows for “typed structures”:<br><strong>$to = { _type: ‘developer’, name: ‘harry’ };<br></strong>This is equivalent to:<strong><br>$to = struct( developer: {name: ‘harry’} );<br></strong>In fact, the function struct() just adds the <em>_type</em> property to the object.(Untyped structures will set <em>_type</em> to null, and <em>valtype()</em> will return <em>stdClass</em> for these.)</p><p>The dot-operator has now two functions — accessing an object’s property, or concatenating strings. If any of the two character immediately next to the dot is a not alphabetic, or the preceding symbol is not a variable, it is seen as concatenation. <br>If in the form $variable.symbol, it is used as an object operator.<br><strong>disp( $to.name . ‘ is a ’ . valtype( $to ) )<br></strong>returns: harry is a developer</p><p>Lets try:<br><strong>display( [1,2,3] );</strong><br>If the below definition is not added, and we did not define a “base” function ( a non-overloaded function), you will get: <br><em>Exception: overload signature not found: val(list)</em><br>This indicates that display() could not find an appropriate <em>val()</em> with which to display the array — that takes a “list” as argument.<br>You can resolve this by either defining a root function (no <em>overload</em>), which will handle all un-catered types, or by defining an overload:<br><strong>overload val( list $x ) ==&gt; ‘list=[’ . join( ‘,’ , $x ) . ‘]’;</strong></p><p>Even more complex signatures — multiple parameters</p><p>The pattern of arguments forms the “signature” of the overload to call.<br>If not found, overload resolution will drop the last type, and search again, until a root overload is reached. Of none is found, it will fail and throw an exception as per the above examples.<br>This shortening of the signature we will call <em>degenerative overloading</em>.</p><pre>overload function do_x( integer $i ) { disp( &quot;do1int: $i&quot; ); }<br>overload function do_x( integer $i, integer $j ) { disp( &quot;do2int: $i $j&quot; ); }<br>overload function do_x( integer $i, integer $j, integer $k ) { disp( &quot;do3int: $i $j $k&quot; ); }<br>overload function do_x( string …$s ) { disp( &quot;do strings:&quot;, …$s ); } <br> do_x(2);        //do1int: 2<br> do_x(2,3);      //do2int: 2 3<br> do_x(2,3,4);    //do3int: 2 3 4<br> do_x(2,3,4,5);  //do3int: 2 3 4 //degenerative match<br> do_x( &#39;a&#39;,&#39;b&#39;,&#39;c&#39; ); //do strings: a b c  //degenerative match</pre><p>Some limitations of overloading:<br>- Only the last parameter of overload functions may be untyped<br>- You cannot use ‘mixed’ to match any type<br>- The argument signature can only be larger than the function signature, therefore default values and optional parameters are not applicable.</p><p>Degeneracy approaches inheritance. <br>If the arguments’ signature does not match, overloading tries a degenerative signature. But it also can use a <em>signature expression.</em> <br>Consider:</p><pre>overload area( shape $s) ==&gt; &#39;undefined&#39;;<br>overload area( circle $s) ==&gt; &#39;circle area is:&#39; . pi() * $s.radius ** 2;<br>overload area( rectangle $s) ==&gt; &#39;rectangle area is:&#39; . $s.width * $s.height;<br>overload area( square $s) ==&gt; &#39;square area is:&#39;. $s.length ** 2;<br>//making some simple overloaded calls:<br>disp( &#39;circle:&#39;,     area( struct( circle:   { radius:10 } ) ) );<br>disp( &#39;rectangle:&#39;,  area( struct( rectangle:{ width:10, height:10 } ) ) );<br>disp( &#39;square:&#39;,     area( struct( square:   { length:10 } ) ) );</pre><p>Now if we consider Moina defining the overload signature to delimit the types with an underscore, we could have tried this:<br><strong> disp( area( struct( shape_square: { length:10 } ) ) );<br></strong>It will attempt to use a ‘shape_square’ overload, and then degenerate to a ‘shape’ overload. Perhaps not the intended idea.</p><p>But there are also some useful operators, similar to <em>union types</em> on parameters. The following call will use <em>square</em> then <em>shape</em>, to find an overload, as <em>alternatives:</em><br><strong>disp( area( { _type: ‘square|shape’, length:10 } ) ); </strong><br>Here the <em>_type</em> syntax must be used, as <em>square|shape</em> is not a valid parameter name. ‘a|b_1|2’ will try a_1, a_2, b_1 and lastly b_2.<br>The following call will use a <em>square</em> overload, then <em>shape, </em>as if shape is a super type, and square a sub type:<br><strong> disp( ‘shape then square:’, area( { _type: ‘shape&gt;square’, length:10 } ) );</strong></p><p>The following call will use a square overload, then <em>shape,</em> in sequence as listed:<br><strong> disp( ‘square,shape:’, area( { _type: ‘square,shape’, length:10 } ) );</strong></p><p>No — these are not all the same. For example the following type expression: <em>string&gt;staff_name|surname, client_name|surname, </em><br>will evaluate to overloads searched for, in this order:<br> [0] =&gt; staff_name<br> [1] =&gt; staff_surname<br> [2] =&gt; string<br> [3] =&gt; client_name<br> [4] =&gt; client_surname<br>These operators has hi to lo priority: _ | &gt; ,</p><p>You can also use type expressions when overloading a function: <br><strong>overload do_x( staff|client …$s ) ==&gt; “do staff/client: “ . str_struct( …$s ); <br></strong>which will serve:<strong><br></strong> <strong>disp( do_x( struct( staff: {name:’harry’} ) ) ); </strong>//and also<strong><br>disp( do_x( struct( client: {name:’harry’} ) ) );</strong></p><p>This allows multiple inheritance, where an object has access to multiple “parent” versions, but defined per function, or per object itself.</p><p>For completeness sake, there are alternative mechanisms in native PHP, which allows polymorphism inside a function.<br>The first uses getType() on an untyped parameter, which will return the native type of the value passed:<br><strong>function do_show( $x ) {<br> if ( getType($x) === ‘integer’ ) echo ‘integer:’ . $x . ‘&lt;br&gt;’;<br> if ( getType($x) === ‘string’ ) echo ‘string:’ . $x . ‘&lt;br&gt;’;<br> }<br></strong>This approach limits the polymorphism to native PHP types, does not allow new types to be processed, and you need to modify the existing do_show() function. In contrast, OOX does allow new forms of the function, without<br>modifying existing code.</p><p>The second alternative is to use named parameters with a variadic function. The passed values are packaged in an associative array.<br><strong> function do_show( …$x ) {<br> if ( isset( $x[‘int’] ) ) echo ‘int:’ . $x[‘int’] . ‘&lt;br&gt;’;<br> if ( isset( $x[‘str’] ) ) echo ‘str:’ . $x[‘str’] . ‘&lt;br&gt;’;<br>}<br></strong> This approach is not limited to native or even define types, but totally dynamic. It does require the arguments be named correctly though.<br>It also does not isolate previously written code like an overload approach does.</p><p>Let us visit our shapes again. Consider:</p><pre>//Here we have a base function and overloads for 3 shapes<br>  overload area( shape $s ) ==&gt; &#39;shape area undefined: &#39; . print_r($s, true); <br>  overload area( circle $s ) ==&gt; &#39;the area of this circle is &#39; . pi() * $s.radius ** 2; <br>  overload area( square $s ) ==&gt; &#39;the area of this square is &#39; . $s.side ** 2;<br>  overload area( rectangle $s ) ==&gt; &#39;the area of this rectangle is &#39; . $s.width * $s.height;<br><br>//now we repeat the overload for the circle<br>overload area( circle $s) ==&gt; &#39;the area of this circle v2 is ...&#39; ;<br>//this in effect replace the previous overload<br>//output: the area of this circle v2 is ...</pre><p>But what would happen if we did this:<br><strong>extend area( circle $s ) ==&gt; ‘the area of this circle v3 is …’ ;<br></strong>Nothing! Because to replace an overload, you must overload it again.<br>This extends a base function (!!!), and engage PHP’s type checking for a type called ‘circle’.<br>If we now call it with a non-overloaded type, it will attempt to call the base function, and will thow a type error: <em>TypeError: area_6_(): Argument #1 ($s) must be of type circle, stdClass given.<br></em>Removing the circle type in the extend, opens up the base function to all types again.</p><h3>Some syntax enhancement:</h3><p><strong>for $i=1..10 {…}<br></strong>This will be replaced with : $_ii_ = 10; for( $i = 0; $i ≤ $_ii_; $i++) { … }<br>Making simple for-loops more elegant. You can even do this: <br>for $i = $i .. $i+1; which will execute exactly twice.</p><p><strong>enum { a, b, c }</strong><br>will define constants a, b and c, equal to 1,2 and 4 respectively.</p><p><strong>==&gt;</strong><br>double-fat-arrow functions — for lambdas and all function, makes the “fn” and “function” redundant.</p><p><strong>{}</strong><br>To define <em>structures, </em>defined as stdObjects — “objects” without methods.<br>If it follows any of these chars <strong>: , = (</strong> the curly brackets is replaced with a call to obj() which wraps the content as a stdObject.</p><p><strong>case()</strong><br>A syntactical simple switch():<br><strong>case ( $x ) {<br> 0: echo ‘zero’;<br> 1,2,3: echo ‘1,2, or 3’;<br> else: echo ‘some other number’;<br>}<br></strong>It does a <em>break</em> after the last statement of each condition set.</p><h3>An lastly, I find a few functions useful :</h3><pre>substring( $string, $from $to ) <br>substring_replace( $str, $ins, $from, $to ) <br>  // gets or display $from up to and including $to.<br>  // $from and $to can be negative, or string delimiters  <br>strposrev( $hay, $needle, $from ) // search towards $hay[0] <br>  //ignoring all after and at $from<br>str_paired( $str, $from, $bra=&#39;(&#39;, $ket=&#39;)&#39; ) //get paired closing bracket<br>array_map_keys( fn($k,$v), $A) // does a map, passing key and value<br>split( $d, $s, $n=0 ) // does explode, and if n&gt;0 return exactly $n elements<br>splitt( $d, $s, $n=0 ) // same as split(), but trim and filter elements<br>hide($s)   // set bit 8 on all chars in a string<br>unhide($s) // clear bit 8 on all chars in string<br>formstr( string $t, $p, $truc=true ) //does formatted interpolation<br>  /* $t is the template, $p is an object or associative array<br>    Expressions can be evaluated into $p.<br>    $trunc controls if fields may be truncated if too long    <br>    &quot;{name=harry}&quot; template can provide a default<br>    &quot;{name} {name%9} {name%9.9} {name%x9.9}&quot; where x can be: n/d/s/f<br>    %n number %n99.9 width.decimals never truncated<br>    %n, number with thousands separator, never truncated<br>    %d date &#39;yyyy-mm-dd HH`hmi:ss dow Month Mon fff yy hh m h w&#39; never trunc<br>    %s string may be truncated<br>    %f uses sprintf() to format<br>  */</pre><h4>In summary</h4><p>Implementing new syntax, and even extending OO concepts to functions, is quite possible. Indeed, it can be syntactical much less complex. The same modularity achieved in OO, is then possible with functions, as one can extend or overload a function, without touching the previous code. This alleviate the need to test the existing code again.</p><p>Simpler syntax always wins. The general problem with web-applications, as is typically what PHP is used for, is not speed or memory limitations. The major issue is maintainability. Here reading the code, and understanding what the developer <em>wanted</em> to do, is crucial.</p><p>As such, I might just be using Moina for simpler syntax, and modularity in my next project…</p><p>You can access and use this pre-parser from here:<br><a href="https://github.com/marxharryhm-hub/PHP-overloading">https://github.com/marxharryhm-hub/PHP-overloading</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea428fd58bb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Longest common substring — DIY on PHP]]></title>
            <link>https://medium.com/@marxharry.hm/longest-common-substring-diy-on-php-c01ea0718c24?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c01ea0718c24</guid>
            <category><![CDATA[longest-substring]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Thu, 02 Apr 2026 13:32:06 GMT</pubDate>
            <atom:updated>2026-04-13T10:50:52.245Z</atom:updated>
            <content:encoded><![CDATA[<p>Much have been written about this, and the optimal solution is presented as searching through a “suffix tree”. But setting up a suffix tree is daunting.<br>And then I do not see how this is going to help with strings in the order of 100k characters. I spent days looking at this, and so far I have not been able to build a suffix tree fast, or search for a solution — yes, perhaps I am too old…</p><p>The search through suffix trees “might” be fast, but building them is at lease O(n*n + n). Even going through Ukkonen’s algorithm ( presented as the most optimal), you must consider each possible suffix, which numbers as N(N+1)/2. Which is just calling it something else than LCS, and dropping it from the loop counting. This concept of “optimal” is reflecting how the search space grows with growing input, not at all how long it will take.</p><p>So I investigated other solutions. Here is a simple, yet not too bad algorithm. Yet, it does not do 100k in real-time… for 10k characters it is ok, doing it in roughly in 50–60ms on my laptop.</p><pre>function str_LCS( $A, $B ) { global $loopc;<br> $X = []; //index for each char in B, <br> $P = -1; //where longest LCS was found<br> $C = 0;  //longest LCS so far<br> $lenA = strlen($A); //just some pre-loop optimization<br> $lenB = strlen($B);<br> for ($j = 0; $j &lt; $lenB; $j++) $X[ $B[$j] ][] = $j; //index each char in B<br> for ($i = 0; $i + $C &lt; $lenA; $i++) { //for each char in A<br>  if ( isset( $X[ $A[$i] ] ) ) {  <br>   foreach ( $X[ $A[$i] ] as $j ) { //for each occurance in B<br>    if ( $j + $C &gt;= $lenB ) break; // Are then even enough chars to lengthen C<br>    if ( $A[ $i + $C ] == $B[ $j + $C ] ) { // check if C+1 char can lengthen C<br>     $p = $i; $q = $j; //now we check all chars until a mismatch<br>     while ( ++$p &lt; $lenA &amp;&amp; ++$q &lt; $lenB &amp;&amp; $A[$p] == $B[$q] ) { $loopc++;};         <br>     if ($p - $i &gt; $C) { //if stretch is longer<br>      $C = $p - $i; $P = $i; <br>     }        <br>    } <br>   }<br>  }<br> }<br> return obj( pos:$P, len: $C );<br> }</pre><p>But the problem is of course that the time it takes, grows exponentially. For 50k characters, it takes about 1.3 seconds. I want to use this to compare and calculate deltas between text files of 100K, and then it is too slow <br>(5.2 seconds) (my users are not patient).</p><p>The best solution I found so far, searches two 100K strings in 0.5 to 1.2s. These random strings were built with a 68 charset, and 10K chars randomly changed. The search speed depends a lot on this. The worst case — where both strings are random, takes about 55s.</p><p>So how does this works?</p><p>I do this in three steps:<br>First I find the two characters in the center of each string, <br>Then I traverse each center character against the whole of the other string,<br>And then repeat the process on each combination of the halves of both strings.</p><p>The code is a bit more lengthy, but easy to understand. This is recursive algorithm, so first there is the base cases, then the calculations, and then implications of the larger problem in to smaller ones.</p><pre>function str_LCS( $a, $b, $ofs=0 ) {<br> global $out;<br> $out ??= asRecord( pos:-1, len: 0);<br> $lenA = strlen($a);<br> $lenB = strlen($b);<br><br> //base cases<br> if ( $lenA &lt; $out-&gt;len || $lenB &lt; $out-&gt;len ) return;   <br> if ( $a === $b ) { $out-&gt;pos = $ofs; $out-&gt;len = $lenA; return; }<br> if ( $lenA === 1 ) { <br>  if ( ( $i = strpos( $b, $a ) ) !== false ) <br>    { $out-&gt;pos = $ofs; $out-&gt;len = 1; }<br>  return;<br> }<br> if ( $lenB === 1 ) {<br>  if ( ( $i = strpos( $a, $b ) ) !== false ) <br>      { $out-&gt;pos = $ofs + $i; $out-&gt;len = 1; }<br>  return;<br> }  <br> //split block into quadrants<br> $cA = $lenA &gt;&gt; 1;<br> $cB = $lenB &gt;&gt; 1;  <br> //traverse A on B&#39;s center  <br> $n = -1;<br> while ( ( $n = strpos( $a, $b[$cB], $n+1 ) ) !== false) {  <br>  for( $p = $n-1, $q = $cB-1; $p &gt;= 0 &amp;&amp; $q &gt;= 0 &amp;&amp; <br>    $a[$p] === $b[$q]; $p--, $q-- );  <br>  if ( $p &lt; $lenA - $out-&gt;len &amp;&amp; $q &lt; $lenB - $out-&gt;len ) {<br>   for( $u = $n+1, $v = $cB+1; $u &lt; $lenA &amp;&amp; $v &lt; $lenB &amp;&amp; <br>      $a[$u] === $b[$v]; $u++, $v++ );<br>   if ( $u - $p - 1 &gt; $out-&gt;len ) {<br>    $out-&gt;len = $u - $p - 1;<br>    $out-&gt;pos = $ofs + $p + 1;<br>   }<br>  }  <br> }<br> //traverse B on A&#39;s center<br> $n = -1;<br> while ( ( $n = strpos( $b, $a[$cA], $n+1 ) ) !== false) {  <br>  for( $p = $cA-1, $q = $n-1; $p &gt;= 0 &amp;&amp; $q &gt;=0 &amp;&amp; <br>    $a[$p] === $b[$q]; $p--, $q--);<br>  if ( $p &lt; $lenA - $out-&gt;len &amp;&amp; $q &lt; $lenB - $out-&gt;len ) {<br>   for( $u = $cA + 1, $v = $n+1; $u &lt; $lenA &amp;&amp; $v &lt; $lenB &amp;&amp; <br>      $a[$u] === $b[$v]; $u++, $v++);<br>   if ( $u - $p - 1 &gt; $out-&gt;len ) {<br>    $out-&gt;len = $u - $p - 1;<br>    $out-&gt;pos = $ofs + $p + 1;<br>   }<br>  } <br> }<br>//here is the recursive part, searching each quadrant<br> $L = substr($a, 0, $cA); $R = substr($a, $cA + 1);<br> $U = substr($b, 0, $cB); $D = substr($b, $cB + 1);<br> if ( $cA &gt; $out-&gt;len &amp;&amp; $cB &gt; $out-&gt;len ) <br>    str_LCS( $L, $U, $ofs );<br> if ( $lenA - $cA - 1 &gt; $out-&gt;len &amp;&amp; $lenB - $cB - 1 &gt; $out-&gt;len ) <br>    str_LCS( $R, $D, $ofs + $cA + 1 );<br> if ( $lenA - $cA - 1 &gt; $out-&gt;len &amp;&amp; $cB &gt; $out-&gt;len ) <br>    str_LCS( $R, $U, $ofs + $cA + 1 );<br> if ( $cA &gt; $out-&gt;len &amp;&amp; $lenB - $cB - 1&gt; $out-&gt;len ) <br>    str_LCS( $L, $D, $ofs );  <br><br> }//function str_LCS()</pre><p>Perhaps you can see how to optimize it more?<br>I do not really know why it is running so fast, my guess is searching through the center of the strings gets a good solution quickly, and then a lot of alternatives can be pruned. Perhaps it is something to investigate.</p><p>Anyway, it runs fast enough for what I want, so next time I will tell you how I build a delta using it.</p><p>Well, in truth, I realized there is a “shortcut” I can do, by virtue of the files being text files, that I want to compare. So instead of doing a character level comparison, I do a line for line comparison. This is good enough for comparing code, which for the most part stays the same. Now of course it runs VERY fast :-).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c01ea0718c24" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PHP, overloading and overriding functions (again )(finally)]]></title>
            <link>https://medium.com/@marxharry.hm/php-overloading-and-overriding-functions-again-66f48c39eab9?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/66f48c39eab9</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[overriding]]></category>
            <category><![CDATA[overloading]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Tue, 24 Feb 2026 13:57:47 GMT</pubDate>
            <atom:updated>2026-03-10T10:37:57.083Z</atom:updated>
            <content:encoded><![CDATA[<p>I revisit this topic often, but I also think I have reached a point to note.<br>Imagine in PHP doing this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*87HDk-_vztG43JocDh1ITQ.png" /><figcaption>and</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XWv2xbU-qGhSwnEkqThz2Q.png" /></figure><p>The 1st line defines <em>str(),</em> that will be called if the value passed is a <em>string</em>.<br>The 2nd line defines another <em>str(),</em> that will called if the value passed is an <em>integer</em>.<br>And so on.</p><p>The override instruction replaces the default behaviour of strpos() !!! ???<br>Yes, this is not suggested, but for some other functions it might be ok…</p><p>This “breaks” a number of “rules” in PHP, i.e. it enhances PHP to do:<br>function overloading, and function overriding. Note — not the OO stuff, this is pure functions.</p><p>So how can this be done? Not trivial, but not too complicated either.</p><p>Let us look at <em>overload()</em> first.<br>The process is:<br>1. alter the function name to include the types<br>2. if not already defined, create a router function that calls the alternate form of the function, based on passed arguments’ types<br>3. replace all calls to the overloaded function with a call to the router</p><p>The router for the overloaded versions is bit complex — it gets the types of the values passed in, append it to the function name, and call the appropriate stored code if it exists. (Note it uses <em>vartype()</em> to get the types… I will get back to this.) We also allow for this signature to degenerate — if called with (string, string), and no such signature is found, then the router may consider overloads defined as (string, … ).<br>These ellipsis we define to match “any number of any types”, if a perfect match is not found.<br>And then just for good measure, if no match is found, we check for a native version.</p><p>Now, for the “trick” to get all this working. The problem in general is that functions are parsed first, and then you cannot redefine these, in code.<br>So consider a pre-processor, linked in php.ini to auto_prepend_file, that reads the source code, do all the overloads and overrides, removing these non-standard PHP from the source, and adding the working code.<br>Then lastly it executes the remaining source.</p><p>Also nice with the overloading, if you call it with a signature it cannot match, it returns:<br><strong>Fatal error</strong>: Uncaught Exception: no overload: str(integer)</p><p>disp() here is a function that display all arguments handed to it, using the overloaded str() function to format it:</p><pre>disp( &quot;hello world&quot; );<br>disp( 123.456 );<br>disp( 123 );<br>disp( [&#39;a&#39; =&gt; 1,2,3] );<br>disp( [1,2,3] );<br>disp( (object)[&#39;x&#39;=&gt;&#39;z&#39;] );<br>disp( true );</pre><p>Now outputs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*Ifsxwl7TZ7toOWhDGvJ8-Q.png" /></figure><p>This all I captured in a preload processor called prePHP.php.<br>If you want to play with it, just ask in the comments and I will send it to you. Its 20k, and include documentation and some additional helpful functions like formstr(), which does formatted interpolation.<br>And a javascript style online object format:</p><pre>$t = { name: &#39;harry&#39;, car: &#39;ford&#39; };</pre><p>Now lets get back to <em>vartype(), </em>and for that matter <em>str()</em>. These are functions, defined in the prePHP engine. And the functions <em>disp()</em> and the router fuctions, uses these. In these examples, I am overloading <em>str()</em>, and overriding <em>vartype()</em>, in the page that is being called. So the code in the prePHP engine uses the native definitions, but the main code use the overloaded/overridden alternatives. What about <em>disp() </em>and the routers?<br>They use the NEW versions!!! This is a bit mind-blowing. You can define a “prototype” of the function, and then later enhance it piecewise as needed. Sounds exactly like the sales pitch for OO does it not? But here we do it with functions, and no OO overhead…</p><p>And that is IT. <br>(at least for now…)</p><p>Ask in the responses, and I will send you the code :-D, free, no royalties, no license! :-)</p><p>Update: the documentation:</p><pre>&lt;?php<br>$prePHP_version = &#39;2; built: 7 March 2026 - implements OOX&#39;;<br>/*<br> Developer: Harry Marx marxh2@unisa.ac.za  <br> OOX - Overload, Override and eXtend<br><br> overload:: the creation of alternative forms of the same function, <br>    servicing a different set of types of values passed as arguments.<br> override:: to create a function that replaces previous definitions of it.<br> extend:: to extend the functionality of an existing function, providing <br>  new code, and allowing to call the previous version of this function <br>  as part of the new code.<br><br> To &quot;override&quot; a function, you simply define it again. This new defintion <br> becomes the &quot;extended&quot; version of it. It means redeclarations of function<br> are now allowed.<br> To access the overridden version, extend the function name in the <br> extended function, with a single underscode. <br> You can only access an overridden function, from its own extended version.<br><br> import()<br>  use import instead of include/require, if the include contains OOX syntax.<br><br> overload function name( typed paramters, untyped parameters ) {} )<br><br>  Warning:<br>   Overloading / overriding native PHP functions is not advisable. <br>   Doing so will most likely break existing code.<br><br>  signature:: the combination of the name and types of parameters, <br>    of an overloaded function<br>  <br>  Types used in signatures correspond to the values returned by vartype(). <br>  &#39;int&#39; will not work, as vartype(1) will return &#39;integer&#39;.<br><br>  All the types for the arguments passed to an overloaded function, <br>  will be included in the initial required signature.<br>  If not found, the types will be dropped one by one to search with <br>  the least degenerate signature.<br>  All types provided in the signature must match passed types.<br>  You cannot use optional parameters in the signature.<br>  <br>  You can only use untyped parameters for an overloaded function as the <br>  last parameters.<br>  They will not be included in the overload&#39;s signature.<br>  Untypes parameters may be optional, and have default values.    <br><br>  You can override/extend an overloaded function - you have to precede <br>  it again with &#39;overload&#39;. <br><br>  If a non-overloaded function with the same name exists, it will be called <br>  if no overloaded signature matches. <br>  This will then not produce the &quot;signature not found&quot; error.<br><br>  See vartype() - it can be extended/overridden to alter its return values.<br>  <br>  ERROR: in overload function: <br>   if the overload function throws an error, it will report as such.  <br>   <br>  ERROR: overload signature not found:<br>   If the overloaded function is called with arguments that does not <br>  match any defined signature, an error will be thrown.<br>   The reported unmatched signature can then be used to create a new <br>  overload that does service it.<br><br> override/extending functions<br>    <br>  Warning:<br>   Extending / overriding native PHP functions is not advisable. <br>   Doing so will most likely break existing code.<br><br>  You can override any function by merely redeclaring it.<br>  You can override a function multiple times. <br>  By default only the last version will be called.<br><br>  You can make recursive calls to and in OOX functions. <br><br>  You can, only in the extended function, call the immediate previous <br>  version of it. The previous version is called by appending an <br>  underscore to the function name.<br><br><br> Technical issues:<br>  Overriding/overloading/extending (OOX) should not change behaviour of <br>  functions used by the OOX process, and the router functions <br>  (except vartype() ). <br>  These are unaffected by OOX, except for vartype() and str(). <br>  die() does not abort OOX declarations - they will still apply even is <br>  after the die() instruction.<br>  Commenting it out will remove OOX instructions.<br><br> vartype( $variable )<br>  This is a slightly more generic function returning the class names for <br>  objects, and returning the &#39;vartype&#39; property on objects with this property.  <br>  You may override/extend it with new types, which will affect router <br>  functions.<br>  You cannot overload vartype(). You can override/extend it.<br><br>Other functions included in this package:<br><br> disp( ...$variables )<br>  It allows the display of multiple arguments, of various types.<br>  It uses an overloaded function str(), to serve the various types.<br>  It has a base function that will display all types using print_r() <br>  that is not overloaded.<br><br> array_map_keys( $array, $function )<br>  Same as array_map, but with keys. $function takes 2 arguments, <br>  key and value.<br>  A new value is calculated for each element, indexed as a list.<br><br> substring( $string, $from, $to=-1) and <br> substring_replace( $string, $insert, $from, $to, $inclusive) <br>   Returns part of the sting including the $from and $to indexes.<br>   If $from or $to is negative, they are relative to the end of the string, <br>      starting with -1.<br>   if $from is a string, $from will be past the end of the first occurance <br>      of $from.<br>   if $to is a string, $to will be the up to before the first occurance <br>      of $to after $from.<br>   0 indicates the left most char, and -1 the right most char.<br>   The following all returns &#39;34567&#39;<br>    substring( &#39;01234567890&#39;,  3,    7) //including the index chars<br>    substring( &#39;01234567890&#39;,  3,   -4)<br>    substring( &#39;01234567890&#39;, -8,    7)<br>    substring( &#39;01234567890&#39;, -8,   -4)<br>    substring( &#39;01234567890&#39;,&#39;12&#39;,&#39;89&#39;) //excluding the delimiters<br>    substring( &#39;zzz{xxx}xxx&#39;, &#39;{&#39;, &#39;}&#39;) //returns &#39;xxx&#39;<br>  substring_replace()<br>   If $from and $to are integers, replaces the chars in $string with $insert, <br>   starting at $from and ending with $to, both also replaced.<br>   If $from and $to are strings, they are replaced.   <br><br>   substring_replace( &#39;zzz{xxx}zzz&#39;,&#39;a&#39;, 3, 7)     //returns &#39;zzzazzz&#39;<br>   substring_replace( &#39;zzz{xxx}zzz&#39;,&#39;a&#39;, &#39;{&#39;, &#39;}&#39; ) //returns &#39;zzzazzz&#39;<br>  <br> strposrev( $hay, $needle, $from )<br>  strrpos() searches from the index to the end of the string, <br>  while strposrev searches from the index to the beginning of the string.<br>  Returnd false of not found.<br>   strrpos( &#39;.A..4.A....A&#39;, &#39;A&#39;,  4 ) returns 11  <br>   strposrev( &#39;.A..4.A....A&#39;, &#39;A&#39;,  4 ) returns  1 <br><br> str_paired( $string, $from=0, $bra=null, $ket=null)<br>  Searches for the end of the first bracket pair starting at $from.<br>  If $bra is not provided, $bra will uptake the first <br>  of (, {, [ or &lt;.  <br>  If $ket is not provided, it will complement $bra.     <br><br>formstr( $string, $parameters_object )  <br>  String interpolation with formatting.  <br>  $parameters_object is an object with properties coinsiding with <br>  symbols in $string to replace.<br>  To interpolate {x: 123} in string, use the property name in curly<br>    brackets:  &#39;...{x}...&#39;<br>    formatting follows: &#39;...{x%10}...&#39;<br>  no rounding, tuncating:      <br>    no %      ---x---    <br>    %10       ---x         ---    <br>    %-10      ---         x---    <br>    %10       ---123.888888---    <br>    %-10      ---123.888888---    <br>    %-10.3    ---   123.889---    <br>    %-10.3    ---     123.9---    <br>    %-10.3    ---       123---   <br>  rounding, no truncating:   <br>    %n-10.3   ---   123.000---    <br>    %n-10.3   ---12345678901234.000---    <br>    %n-10.3   ---   123.889---    <br>    %n-10.3   ---1234567.889---    <br>    %n-10.3   ---123456789123.889---    <br>    %n-10.3   ---12345678.889---    <br>    %n-10.3   ---1234567898765.889---    <br>    %n,-10.2  ---123,456,789,876.67---   <br>  dates:     <br>    %d        ---2026-03-03 10h54:18 Tue---    <br>    %d10      ---2026-03-03---    <br>    %dd.dd Mon \&#39;yy     <br>              ---03 Mar &#39;26---   <br>  misc:   %s  ---Hello worl---   <br>  uding sprintf formats:   <br>    %f       <br><br>record(...$named_parameters)  <br>  Wraps the named input parameters as an object. <br><br>split( $delimiter, $string, $count=0, $trimmed=false )  <br>  Wraps explode(), and return at least $count elements.  <br>  If trimmed = true, it will trim all elements, and filter <br>  out empty elements.  It will then append null to return $count elements.   <br><br>str()  <br>  This is a &quot;volitile&quot; function, and it will affect OOX functionality if<br>  extended.<br>  Used to convert any passed value into a string, used by disp() <br>  and fomstr().  <br>  You can overload this function for any vartype().   <br><br>inline object syntax:  <br>  Brings PHP in line with javascript syntax for defining objects inline.  <br>  This is used often to pass named, optional parameters, perhaps with <br>  default values.    <br>  ex.:   <br>    echo formstr( &quot;My name is {name} and I am {age} years old.&quot;, <br>      {name:&#39;Harry&#39;} )   <br><br>    function formstr( $stringtoformat, $params )    <br>      $params-&gt;age ??= &#39;21 with 40y experience&#39;; <br>      //and here we add the default values on $params<br>      ...<br><br>*/</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66f48c39eab9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Function overloading in JavaScript]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">Yes, JavaScript is more OO than functional, but hey, lets try something silly.</p><p class="medium-feed-link"><a href="https://medium.com/@marxharry.hm/function-overloading-in-javascript-619239ce5182?source=rss-ed5ec6b995b5------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@marxharry.hm/function-overloading-in-javascript-619239ce5182?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/619239ce5182</guid>
            <category><![CDATA[function-overloading]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Thu, 05 Feb 2026 07:27:52 GMT</pubDate>
            <atom:updated>2026-02-11T04:14:47.809Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[More on “functional” programming]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">The spreadsheet story I wrote made me think more about functional programming (FP). In essence, for what I want to explore, FP has the&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@marxharry.hm/more-on-functional-programming-5c6ebf779baa?source=rss-ed5ec6b995b5------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@marxharry.hm/more-on-functional-programming-5c6ebf779baa?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c6ebf779baa</guid>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Wed, 03 Sep 2025 08:02:21 GMT</pubDate>
            <atom:updated>2025-09-03T08:02:21.556Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[SVG in PHP, an introduction]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@marxharry.hm/svg-in-php-an-introduction-4f524b1539c2?source=rss-ed5ec6b995b5------2"><img src="https://cdn-images-1.medium.com/max/876/1*LszWRS7-tWnrbtY34JUoMQ.png" width="876"></a></p><p class="medium-feed-snippet">I wanted to do some simple 2D line animation&#x200A;&#x2014;&#x200A;and started looking at SVG. So as I learn by coding, naturally I coded a simple SVG-engine&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@marxharry.hm/svg-in-php-an-introduction-4f524b1539c2?source=rss-ed5ec6b995b5------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@marxharry.hm/svg-in-php-an-introduction-4f524b1539c2?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f524b1539c2</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Tue, 04 Mar 2025 13:17:47 GMT</pubDate>
            <atom:updated>2025-03-07T12:52:31.902Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[On coding your own spreadsheet]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@marxharry.hm/on-coding-your-own-spreadsheet-1d239f9b4723?source=rss-ed5ec6b995b5------2"><img src="https://cdn-images-1.medium.com/max/600/1*kSdeJdmWIgET_8wuJkMN2A.png" width="600"></a></p><p class="medium-feed-snippet">Yes, I&#x2019;m mad&#x2026; but imagine you want to do a simple web frontend, using a spreadsheet as template&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@marxharry.hm/on-coding-your-own-spreadsheet-1d239f9b4723?source=rss-ed5ec6b995b5------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@marxharry.hm/on-coding-your-own-spreadsheet-1d239f9b4723?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d239f9b4723</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[spreadsheets]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Tue, 11 Feb 2025 14:10:45 GMT</pubDate>
            <atom:updated>2025-02-11T14:10:45.279Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[GAP — generic web based ERP apps — chapter 4— Safety and Security(PHP)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@marxharry.hm/gap-generic-web-based-erp-apps-chapter-4-safety-and-security-php-f50ba7e42e6a?source=rss-ed5ec6b995b5------2"><img src="https://cdn-images-1.medium.com/max/1152/1*f_0iO3Y7Jp2zdPdDXCYxPg.png" width="1152"></a></p><p class="medium-feed-snippet">You cannot work with ERPs and not think about security. So how will GAP ensure data safety, and information security?</p><p class="medium-feed-link"><a href="https://medium.com/@marxharry.hm/gap-generic-web-based-erp-apps-chapter-4-safety-and-security-php-f50ba7e42e6a?source=rss-ed5ec6b995b5------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@marxharry.hm/gap-generic-web-based-erp-apps-chapter-4-safety-and-security-php-f50ba7e42e6a?source=rss-ed5ec6b995b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f50ba7e42e6a</guid>
            <category><![CDATA[erp-software]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[gap]]></category>
            <dc:creator><![CDATA[Harry Marx]]></dc:creator>
            <pubDate>Thu, 16 Jan 2025 07:56:19 GMT</pubDate>
            <atom:updated>2025-01-16T07:56:19.311Z</atom:updated>
        </item>
    </channel>
</rss>