CodeIgniter Forums
potentially polymorphic call with model instance inside model function - 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: potentially polymorphic call with model instance inside model function (/showthread.php?tid=84685)



potentially polymorphic call with model instance inside model function - objecttothis - 10-31-2022

I'm porting a CI3 app to CI4 and need to call one model method inside another model method.  Can someone tell me why this produces no warning:

PHP Code:
public function get_value(): int
 
{
 
$this->sale model('Sale');
 return 
$this->sale->get_quote_number_for_year();
 }


But if I do this:

PHP Code:
public function get_value(): int
 
{
 
$sale model('Sale');
 return 
$sale->get_quote_number_for_year();
 }


I get a warning from PhpStorm
Potentially polymorphic call. Model does not have members in its hierarchy

The function signature for get_quote_number_for_year() is
public function Sale::get_quote_number_for_year($year = '', $start_from = 0) int


RE: potentially polymorphic call with model instance inside model function - kenjis - 10-31-2022

What if model(Sale::class)?


RE: potentially polymorphic call with model instance inside model function - objecttothis - 10-31-2022

(10-31-2022, 01:47 AM)kenjis Wrote: What if model(Sale::class)?

with
PHP Code:
$sale model(Sale::class); 

The warning goes away. The puzzling thing for me is that the documentation (https://codeigniter.com/user_guide/models/model.html) doesn't give examples this way but gives examples using $modelName =

Maybe this has to do with me instantiating a model inside a model instead of a class? What is the best practice here? $this->sale = model('Sale') or $sale = model(Sale::class)?


RE: potentially polymorphic call with model instance inside model function - kenjis - 10-31-2022

Did you specify the type for $this->sale ?

model(Sale::class) and model('Sale') are exactly the same for CodeIgniter,
but a bit different for PHPStan.

I recommend model(Sale::class) because PHPStan knows the return value type (Sale::class).
And the type is correct more than 99%.


See also https://www.jetbrains.com/help/phpstorm/php-possible-polymorphic-call.html


RE: potentially polymorphic call with model instance inside model function - objecttothis - 10-31-2022

(10-31-2022, 02:43 AM)kenjis Wrote: Did you specify the type for $this->sale ?

model(Sale::class) and model('Sale') are exactly the same for CodeIgniter,
but a bit different for PHPStan.

I recommend model(Sale::class) because PHPStan knows the return value type (Sale::class).
And the type is correct more than 99%.


See also https://www.jetbrains.com/help/phpstorm/php-possible-polymorphic-call.html

I did not specify the type for $this->sale. Just what you see in the code I posted.

I do have a use statement at the top of the file

PHP Code:
namespace App\Models\Tokens;

use 
app\Models\Sale



RE: potentially polymorphic call with model instance inside model function - kenjis - 10-31-2022

<?php

namespace App\Models;

I cannot reproduce the warning.
I got no warning.

PHP Code:
use CodeIgniter\Model;

class 
Sale extends Model
{


PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
Test extends Model
{
    public function get_value(): int
    
{
        $sale model('Sale');

        return $sale->get_quote_number_for_year();
    }


I don't know why you got the warning.