CodeIgniter Forums
Accessing a custom object with instanceof - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Accessing a custom object with instanceof (/showthread.php?tid=78474)



Accessing a custom object with instanceof - baxterheinen - 01-25-2021

I have instantiated a new object from a class in my app/libraries folder as such:

PHP Code:
// in controller
use App\Libraries\LN_Account


PHP Code:
// in controller method
$account = new LN_Account(); 


But when I pass $account to my model and check if it is an instance of LN_Account the conditional fails. 

PHP Code:
// in controller method
$account $this->AuthenticationModel->getAuthenticationCredentials($account); 



PHP Code:
// in model
public function getAuthenticationCredentials($account) {
    if ($account instanceof LN_Account) {
         // I never get here
     }




How do I make LN_Account visible to my model?


RE: Accessing a custom object with instanceof - craig - 01-26-2021

In model:

Code:
use App\Libraries\LN_Account;

You can also do the following in getAuthenticationCredentials() to show details about the variable:

Code:
d($account);



RE: Accessing a custom object with instanceof - baxterheinen - 01-26-2021

I tried adding that to the Model with no luck. Same error. 

I have been using CI3 for like 8 years so not a n00b here, and PHP for 15 years. This one has me stumped.


RE: Accessing a custom object with instanceof - natanfelles - 01-26-2021

LN_Account has the proper use call in the model?


RE: Accessing a custom object with instanceof - baxterheinen - 01-26-2021

The specific error is: Class 'LN_Account' not found

The model is like this:


PHP Code:
namespace App\Models;
use 
CodeIgniter\Model;
use 
App\Libraries\LN_Account;

Class 
AuthenticationModel extends Model {
    public function getAuthenticationCredentials($account) {
        if ($account instanceof LN_Account) {
            $sql "SELECT * FROM ln_authentication WHERE username = ?";
            $query $this->db->query($sql, array($credentials->username));
            $account $query->getCustomRowObject(0'LN_Account');
        }
        return $account;
    }


LN_Account is in app\Libraries

PHP Code:
namespace App\Libraries;

class 
LN_Account {
    ....


if I print_r $credentials inside the model/method I get this, so the controller knows what it is and created it. 

PHP Code:
App\Libraries\LN_Account Object
(
    [id] => 0
    
[username] => admin@domain.com
    
[password] => 
    [password_key] => 0
    
[password_key_timeout] => 0
    
[status] => 
    [created_at] => 0
    
[updated_at] => 0
    
[deleted_at] => 0


Okay so the outer if ($account instanceof LN_Account) seems to work now, it's this that doesn't:

$credentials = $query->getCustomRowObject(0, 'LN_Account')

Do I have to instantiate a new LN_Account() for it to use? I figured it would do it automatically.


RE: Accessing a custom object with instanceof - iRedds - 01-26-2021

(01-26-2021, 06:18 PM)baxterheinen Wrote: The specific error is: Class 'LN_Account' not found

15 years  Big Grin

Of course you will get an error.

PHP Code:
//use 
->getCustomRowObject(0, 'App\\Libraries\\LN_Account');
// or
->getCustomRowObject(0, LN_Account::class); 



RE: Accessing a custom object with instanceof - baxterheinen - 01-26-2021

(01-26-2021, 06:34 PM)iRedds Wrote:
(01-26-2021, 06:18 PM)baxterheinen Wrote: The specific error is: Class 'LN_Account' not found

15 years  Big Grin

Of course you will get an error.

PHP Code:
//use 
->getCustomRowObject(0, 'App\\Libraries\\LN_Account');
// or
->getCustomRowObject(0, LN_Account::class); 


Ah, because it's not already an object, got it.

(01-26-2021, 06:37 PM)baxterheinen Wrote:
(01-26-2021, 06:34 PM)iRedds Wrote:
(01-26-2021, 06:18 PM)baxterheinen Wrote: The specific error is: Class 'LN_Account' not found

15 years  Big Grin

Of course you will get an error.

PHP Code:
//use 
->getCustomRowObject(0, 'App\\Libraries\\LN_Account');
// or
->getCustomRowObject(0, LN_Account::class); 


Ah, because it's not already an object, got it.

Why don't the docs cover this? They say the autoloader will instantiate it. 

https://codeigniter.com/user_guide/database/results.html



RE: Accessing a custom object with instanceof - iRedds - 01-26-2021

(01-26-2021, 06:37 PM)baxterheinen Wrote: Ah, because it's not already an object, got it.

Why don't the docs cover this? They say the autoloader will instantiate it. 

https://codeigniter.com/user_guide/database/results.html

Incorrect example in the documentation.
The documentation uses the User class without a namespace, so just 'User' is passed as a parameter.
If the class uses a namespace, then it must also be specified.


RE: Accessing a custom object with instanceof - InsiteFX - 01-27-2021

This is from PHP.net.

Code:
If you want to test if a classname is an instance of a class, the instanceof operator won't work.

<?php
$classname = 'MyClass';
if( $classname instanceof MyParentClass) echo 'Child of it';
else echo 'Not child of it';
?>

Will always output
Not child of it

You must use a ReflectionClass :
<?php
$classname = 'MyClass';
$myReflection = new ReflectionClass($classname);
if( $myReflection->isSubclassOf('MyParentClass')) echo  'Child of it';
else echo 'Not child of it';
?>

Will output the good result.
If you're testing an interface, use implementsInterface() instead of isSublassOf().