CodeIgniter Forums
Unable to use typed variables in a cell - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Unable to use typed variables in a cell (/showthread.php?tid=90967)



Unable to use typed variables in a cell - Fabio-Zeus-Soft - 05-29-2024

Hi everyone!
I like using Cells in my project, especially the controlled ones. And I'd rather to use typed variables in my project.
But when i try to use typed peroperties in controlled cells, I get 
Code:
Undefined variable $typedVariable

Here is my code (I created a small project just for testing this)
Code:
JustATestCell.php
PHP Code:
<?php

namespace App\Cells;

use 
CodeIgniter\View\Cells\Cell;

class 
JustATestCell extends Cell
{
    public int $typedVariable;


Code:
just_a_test.php
PHP Code:
<div>
    <?= $typedVariable?>
</div> 

Code:
main view file
PHP Code:
...
<?=
    view_cell('JustATestCell', [
            'typedVariable' => 1
        
]);
    ?>
... 

So my question is: what am I doing wrong? Am I supposed not to use typed properties in cells?

Thanks in advance


RE: Unable to use typed variables in a cell - Bosborne - 05-29-2024

How are you passing the variable to the view?


RE: Unable to use typed variables in a cell - InsiteFX - 05-29-2024

View Cells must return a string not an int.


RE: Unable to use typed variables in a cell - Fabio-Zeus-Soft - 05-30-2024

(05-29-2024, 04:00 AM)Bosborne Wrote: How are you passing the variable to the view?

As you can read here, those are made available automatically to the view file, because I'm using controlled cells

(05-29-2024, 05:42 AM)InsiteFX Wrote: View Cells must return a string not an int.

As far as I can see, the Cell is returning a string, because it's returning the content of just_a_test.php.
But for the sake of the conversation, here is the modified version, returning the same error, which is:
Code:
Undefined variable $typedVariable

Code:
JustATestCell.php
PHP Code:
<?php

namespace App\Cells;

use 
CodeIgniter\View\Cells\Cell;

class 
JustATestCell extends Cell
{
    public string $typedVariable;


Code:
just_a_test.php
PHP Code:
<div>
    <?= $typedVariable?>
</div> 
Code:
main view file
PHP Code:
<?=    view_cell('JustATestCell', [
            'typedVariable' => 'test'
        ]);
    ?>



RE: Unable to use typed variables in a cell - kenjis - 05-30-2024

Try:
PHP Code:
class JustATestCell extends Cell
{
    public $typedVariable;

or
PHP Code:
class JustATestCell extends Cell
{
    public int $typedVariable 0;


This is due to the behavior of get_object_vars().
See https://www.php.net/manual/ja/function.get-object-vars.php#127940


RE: Unable to use typed variables in a cell - Fabio-Zeus-Soft - 05-30-2024

(05-30-2024, 05:21 AM)kenjis Wrote: Try:
PHP Code:
class JustATestCell extends Cell
{
    public $typedVariable;

or
PHP Code:
class JustATestCell extends Cell
{
    public int $typedVariable 0;


This is due to the behavior of get_object_vars().
See https://www.php.net/manual/ja/function.get-object-vars.php#127940

This was pretty cool, good job, many thanks, much appreciated!
Could be useful then to use the Reflection API to improve this function?
I mean CodeIgniter\Traits\PropertiesTrait->getPublicProperties(), instead of relying on get_object_vars() should leverage the advantages of the Reflection API over get_object_vars()