Welcome Guest, Not a member yet? Register   Sign In
Unable to use typed variables in a cell
#1
Bug 
(This post was last modified: 05-29-2024, 02:14 AM by Fabio-Zeus-Soft.)

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
Reply
#2

How are you passing the variable to the view?
Reply
#3

View Cells must return a string not an int.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 05-30-2024, 04:28 AM by Fabio-Zeus-Soft.)

(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'
        ]);
    ?>
Reply
#5

(This post was last modified: 05-30-2024, 05:24 AM by kenjis.)

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.g...php#127940
Reply
#6

(This post was last modified: 05-30-2024, 08:13 AM by Fabio-Zeus-Soft.)

(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.g...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()
Reply




Theme © iAndrew 2016 - Forum software by © MyBB