2021/11/24/things you can't do in PHP

From Woozle Writes Code
Jump to navigation Jump to search
Codeblog

Take 1: my bad

This is where I started -- and admittedly there was already a problem in that I'm useing taScalarAccess twice in cScalarReadOnly (once each via cScalarLocal and taReadOnlyScalar). The idea, though, was to override the SetIt() and ClearIt() methods in whatever class was using taReadOnlyScalar. Those methods being implemented in cScalarLocal, the use of the taReadOnlyScalar trait was intended to rename those methods with a "_" prefix and call them conditionally from the replacement methods.

 2 trait taScalarAccess {
 3     abstract public function SetIt(mixed $v);
 4     abstract public function GetIt() : mixed;
 5     abstract public function HasIt() : bool;
 6     abstract public function ClearIt();
 7 }
 8 trait tScalarLocal {
 9     use taScalarAccess;
10     public function GetIt() : mixed {}
11     public function HasIt() : bool {}
12     public function SetIt(mixed $v) {}
13     public function ClearIt() {}
14 }
15 trait taReadOnlyScalar {
16     use taScalarAccess {
17       taScalarAccess::SetIt as protected _SetIt;
18       taScalarAccess::ClearIt as protected _ClearIt;
19       }
20     public function SetIt(mixed $v) { if ($something) { $this->_SetIt($v); } }
21     public function ClearIt() { if ($something) { $this->_ClearIt(); } }
22 }
23 
24 class cScalarLocal {
25     use taScalarAccess;
26     use tScalarLocal;
27 }
28 class cScalarReadOnly extends cScalarLocal {
29     use tScalarLocal;
30     use taReadOnlyScalar;
31 }

Result

PHP Fatal error: Trait method SetIt has not been applied, because there are collisions with other trait methods on cScalarReadOnly in /home/htnet/site/git/ferreteria/base/tests/php2.php on line 28

Take 2: decouple the conflicting traits

First, I tried moving use taScalarAccess out of tScalarLocal, even though I wanted the latter to descend from the former because of a method implemented in the former (not shown). Classes that use tScalarLocal would also need to explicitly use taScalarAccess in order to get that functionality.

 2 trait taScalarAccess {
 3     abstract public function HasIt() : bool;
 4     abstract public function GetIt() : mixed;
 5     abstract public function SetIt(mixed $v);
 6     abstract public function ClearIt();
 7 }
 8 trait tScalarLocal {
 9     public function HasIt() : bool {}
10     public function GetIt() : mixed {}
11     public function SetIt(mixed $v) {}
12     public function ClearIt() {}
13 }
14 trait taReadOnlyScalar {
15     use taScalarAccess {
16       taScalarAccess::SetIt as protected _SetIt;
17       taScalarAccess::ClearIt as protected _ClearIt;
18       }
19     public function SetIt(mixed $v) { if ($something) { $this->_SetIt($v); } }
20     public function ClearIt() { if ($something) { $this->_ClearIt(); } }
21 }
22 
23 class cScalarLocal {
24     use taScalarAccess;
25     use tScalarLocal;
26 }
27 class cScalarReadOnly extends cScalarLocal {
28     use tScalarLocal;
29     use taReadOnlyScalar;
30 }

Result

PHP Fatal error: Trait method SetIt has not been applied, because there are collisions with other trait methods on cScalarReadOnly in /home/htnet/site/git/ferreteria/base/tests/php2.php on line 27

i.e. that didn't fix the problem.

Take 3: interface

Next, I tried moving the abstract functions from taScalarAccess into an interface, to avoid the collision. (Had this worked, it would have been nice if traits could implement interfaces, but it didn't so the point is moot as far as this issue goes.)

 2 interface ifScalarAccess {
 3     function HasIt() : bool;
 4     function GetIt() : mixed;
 5     function SetIt(mixed $v);
 6     function ClearIt();
 7 }
 8 trait taScalarAccess {}
 9 trait tScalarLocal {
10     public function HasIt() : bool {}
11     public function GetIt() : mixed {}
12     public function SetIt(mixed $v) {}
13     public function ClearIt() {}
14 }
15 trait taReadOnlyScalar {
16     use taScalarAccess {
17       taScalarAccess::SetIt as protected _SetIt;
18       taScalarAccess::ClearIt as protected _ClearIt;
19       } //*/
20     public function SetIt(mixed $v) { if ($something) { $this->_SetIt($v); } }
21     public function ClearIt() { if ($something) { $this->_ClearIt(); } }
22 }
23 
24 class cScalarLocal implements ifScalarAccess {
25     use taScalarAccess;
26     use tScalarLocal;
27 }
28 class cScalarReadOnly extends cScalarLocal {
29     use tScalarLocal;
30     use taReadOnlyScalar;
31 }

Result

PHP Fatal error: An alias was defined for taScalarAccess::SetIt but this method does not exist in /home/htnet/site/git/ferreteria/base/tests/php2.php on line 15

Take 4: hide the real write fx()

Here's what I ended up with the next day, including bits of code excluded in the examples above. I basically started with the _SetIt() and _ClearIt() functions being defined even where we're just going to call them directly from SetIt() and ClearIt(), which makes it easy to just override the latter two so they can act conditionally where needed.

 2 abstract class caScalarAccess {
 3     // DIRECT: WRITE
 4     abstract protected function _SetIt(mixed $v);
 5     abstract protected function _ClearIt();
 6   
 7     // API: WRITE (default)
 8     public function SetIt(mixed $v) { return $this->_SetIt($v); }
 9     public function ClearIt() { $this->_ClearIt(); }
10     // API: READ
11     abstract public function HasIt() : bool;
12     abstract public function GetIt() : mixed;
13     public function GetItNz($default=NULL) { return $this->HasIt() ? $this->GetIt() : $default; }
14     
15     // IDE
16     public function DumpLine() : string {
17         $bHas = $this->HasIt();
18         $sVal = $bHas ? (values\csFormats::Render($this->GetIt())) : cEnv::ItalIt('(not set)');
19         $ftClass = cEnv::BoldIt(get_class($this));
20         $out = "Piece class: $ftClass Value: [$sVal]";
21         return $out;
22     }
23 }
24 // PURPOSE: value is stored internally
25 trait tScalarLocal {
26     private $tscl_value;
27     private $tscl_exists = FALSE;
28   
29     public function HasIt() : bool { return $this->tscl_exists; }
30     public function GetIt() : mixed { return $this->tscl_value; }
31     protected function _SetIt(mixed $v) {
32         $this->tscl_value = $v;
33         $this->tscl_exists = TRUE;
34     }
35     protected function _ClearIt() {
36         $this->tscl_exists = FALSE;
37         #unset($this->value);
38     }
39 }
40 /*::::
41   REQUIRES: tScalarLocal
42   * We can't just "use" that here, though, because cScalarReadOnly ends up using both of them.
43 */
44 trait tScalarReadOnly {
45   
46     private $canWrite = FALSE;
47     
48     protected function SetCanWrite(bool $b) { $this->canWrite = $b; }
49     protected function GetCanWrite() : bool { return $this->canWrite; }
50 
51     // ++ API ++ //
52     
53     public function SetIt(mixed $v) {
54         if ($this->GetCanWrite()) {
55             $this->_SetIt($v);
56         } else {
57             $this->ThrowWriteError('set');
58         }
59     }
60     public function ClearIt() {
61         if ($this->GetCanWrite()) {
62             $this->_ClearIt();
63         } else {
64             $this->ThrowWriteError('clear');
65         }
66     }
67     
68     // -- API -- //
69     // ++ ERROR ++ //
70     
71     protected function ThrowWriteError(string $sAction) {
72         $sClass = get_class($this);
73         $sError = "trying to $sAction the value of a read-only $sClass object.";
74         $e = new except\cUsage($sError);
75         throw $e;
76     }
77     
78     // -- ERROR -- //
79 }
80 
81 class cScalarLocal { use tScalarLocal; }
82 /*::::
83   THINKING: This is neither writeable nor a reference, so local storage should always be sufficient.
84 */
85 class cScalarReadOnly extends cScalarLocal {
86     use tScalarLocal;
87     use tScalarReadOnly;
88     
89     public function __construct(mixed $value=NULL, bool $exists=FALSE) {
90         $this->SetWritable(FALSE);
91         if ($exists) {
92             $this->_SetIt($value);
93         } else {
94             $this->_ClearIt();
95         }
96     }
97 }