| 2835 | class PassByReferenceDemo |
| 2836 | { |
| 2837 | public function demo(): void |
| 2838 | { |
| 2839 | // When a function takes a typed &$var parameter, the variable |
| 2840 | // acquires that type after the call. |
| 2841 | initPen($pen); |
| 2842 | $pen->write(); // $pen is now Pen |
| 2843 | |
| 2844 | // Static method calls with by-ref parameters: |
| 2845 | PenFactory::create($staticPen); |
| 2846 | $staticPen->write(); // $staticPen is now Pen |
| 2847 | |
| 2848 | // Constructor calls with by-ref parameters: |
| 2849 | new PenBuilder($ctorPen); |
| 2850 | $ctorPen->write(); // $ctorPen is now Pen |
| 2851 | |
| 2852 | // Instance method calls ($this->method) with by-ref parameters: |
| 2853 | $this->init($thisPen); |
| 2854 | $thisPen->write(); // $thisPen is now Pen |
| 2855 | } |
| 2856 | |
| 2857 | private function init(?Pen &$pen): void |
| 2858 | { |