Welcome Guest, Not a member yet? Register   Sign In
Accessing a custom object with instanceof
#1

(This post was last modified: 01-26-2021, 12:08 AM by baxterheinen.)

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

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);
Reply
#3

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.
Reply
#4

LN_Account has the proper use call in the model?
Reply
#5

(This post was last modified: 01-26-2021, 06:29 PM by baxterheinen.)

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

(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); 
Reply
#7

(This post was last modified: 01-26-2021, 06:38 PM by baxterheinen.)

(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/datab...sults.html
Reply
#8

(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/datab...sults.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.
Reply
#9

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().
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB