-1

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?

tamak
  • 1,541
  • 2
  • 19
  • 39
  • I added global $commDocsAdminExecutiveUsers; and global $commDocsAdminUsers; at top of my PHP and still get same result. – tamak Jan 18 '17 at 01:52
  • @EdCottrell sure, the questions (in hindsight) have the same answer, but clearly the question isn't 'an EXACT DUPLICATE'. Unless I've bent space and time. Thanks though. – tamak Jan 18 '17 at 02:15
  • @tamak Yes, it really is an exact duplicate. It has the same root cause and same answer because it reflects the same misunderstanding of variable scope. – elixenide Jan 18 '17 at 02:26
  • @EdCottrell thanks Ed – tamak Jan 18 '17 at 02:56

1 Answers1

1

set $commDocsAdminUsers and $commDocsAdminExecutiveUsers to a global variable or a property of class.

class userLogin {

    public static $commDocsAdminUsers = array("kaisersolze", "bennyblanco", "carlitobrigante", "ashylarry", "claytonbigsby");
    public static $commDocsAdminExecutiveUsers = array("waltjr", "skylerwhite", "tuco", "skinnypete", "walterwhite");


    // ALLOWS US TO CHECK THE ACCESS LEVEL OF THE USER... FIND WHICH ARRAY USER IS IN
    function check_access() {
        if (in_array("kaisersolze", self::commDocsAdminUsers)){
          return "Match found in comDocsUsers Array";
        }
        else if (in_array($this->name, self::commDocsAdminExecutiveUsers)){
              return "Match found in comDocsUsers Array";
        }
        else {
              return "No Access Allowed";  // USER NOT FOUND IN EITHER ARRAY
            }
        }
    } 
harry
  • 483
  • 2
  • 12