I'm experimenting with Object Oriented programming in PHP to make more of my PHP code re-usable and more modular.
It finally clicked and I get it and see the benefits of using classes in my code but in a test, ran into an issue I can't quite figure out.
I have two arrays of users' names
$commDocsAdminUsers
$commDocsAdminExecutiveUsers
I have the following class to create a new $currentUser and am trying to use in_array to find which of those two arrays the user is found in.
I keep getting a PHP error that says it doesn't think the arrays are arrays:
Warning: in_array() expects parameter 2 to be array, null given in - on line 25
Here's what I'm using...
<?php
//ARRAYS OF USERS TO CHECK AGAINST
$commDocsAdminUsers = array("kaisersolze", "bennyblanco", "carlitobrigante", "ashylarry", "claytonbigsby");
$commDocsAdminExecutiveUsers = array("waltjr", "skylerwhite", "tuco", "skinnypete", "walterwhite");
class userLogin {
// ALLOWS US TO SET THE NAME OF USER
function set_name($new_name) {
$this->name = $new_name;
}
// ALLOWS US TO GET THE NAME OF USER
function get_name() {
return $this->name;
}
// ALLOWS US TO CHECK THE ACCESS LEVEL OF THE USER... FIND WHICH ARRAY USER IS IN
function check_access() {
if (in_array("kaisersolze", $commDocsAdminUsers)){
return "Match found in comDocsUsers Array";
}
else if (in_array($this->name, $commDocsAdminExecutiveUsers)){
return "Match found in comDocsUsers Array";
}
else {
return "No Access Allowed"; // USER NOT FOUND IN EITHER ARRAY
}
}
}
// NOW LETS DO SOME STUFF WITH OUR NEW CLASS
$currentUser = new userLogin(); // INVOKE THE NEW USER CLASS
$currentUser->set_name("kaisersolze"); // SET NAME OF CURRENT USER
print_r($currentUser); // SHOW THE FULL OBJECT $CURRENTUSER
echo '<BR>-------------------------------------------------------------<BR>';
echo $currentUser->get_name(); // ECHO OUT THE NAME FOR THE $CURRENTUSER
echo '<BR>--------------------------------------------------------------<BR>';
echo $currentUser->check_access(); // CHECK TO SEE WHICH ARRAY THE USER IS IN
?>
How can I successfully run in_array from my class?