14 Replies - 933 Views - Last Post: 18 November 2011 - 05:57 PM Rate Topic: -----

#1 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Undefined variable in OOP PHP

Posted 15 November 2011 - 06:24 PM

These are the error messages I keep getting. The code is below...

Notice: Undefined variable: name in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: hometown in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: location in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: birthday in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: collegeid in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: college in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: majorid in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: major in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: grad in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: company in C:\xampp\htdocs\OOP\teststudent.php on line 60

Notice: Undefined variable: jobtitle in C:\xampp\htdocs\OOP\teststudent.php on line 60

To instantiate the object:

  <?php

    require_once('class.Student.php');
    
    $objStudent = new Student($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests);
    $objStudent->name = $user_profile['name'];
    $objStudent->hometown = $user_profile['hometown']['id'];
    $objStudent->location = $user_profile['location']['id'];
    $objStudent->birthday = $user_profile['birthday'];
    $objStudent->collegeid = $user_profile['education'][1]['school']['id'];
    $objStudent->college = $user_profile['education'][1]['school']['name'];
    $objStudent->majorid = $user_profile['education'][1]['concentration'][0]['id'];
    $objStudent->major = $user_profile['education'][1]['concentration'][0]['name'];
    $objStudent->grad = $user_profile['education'][1]['year']['name'];
    $objStudent->company = $user_profile['work'][0]['employer']['name'];
    $objStudent->jobtitle = $user_profile['work'][0]['position']['name'];
    $objStudent->eduRecommender();
    
    ?>


And the constructor:
 <?php

    class Student {
        
        public $name;
        public $hometown;
        public $location;
        public $birthday;
        public $college;
        public $major;
        public $grad;
        public $company;
        public $jobtitle;
        public $interests;
      
        
        public function __construct($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests) {
            $this->name = $name;
            $this->hometown = $hometown;
            $this->location = $location;
            $this->birthday = $birthday;
            $this->collegeid = $collegeid;
            $this->college = $college;
            $this->majorid = $majorid;
            $this->major = $major;
            $this->grad = $grad;
            $this->company = $company;
            $this->jobtitle = $jobtitle;
            $this->interests = $interests;
        }
        
        function eduRecommender() {
           if ($this->majorid=='104076956295773') {
                ?>
                <h2>You're on the right track!</h2>
                <p>Majoring in Computer Science is definitely a giant leap forward. It's important, however, that you focus your energies on the technologies you'll be utilizing in the workplace at Facebook.</p>
            <?php
                } else {
                ?>
                <h2>You need to major in Computer Science</h2>
                <p>You're currently a <?php echo "$this->major" ?> major at <?php echo "$this->college" ?>. We recommend changing your major to Computer Science as it greatly improves your chances of success. As an alternative, declaring a minor in Computer Science would also be a major help.</p>
            <?php
                }
                ?>



Is This A Good Question/Topic? 0
  • +

Replies To: Undefined variable in OOP PHP

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Undefined variable in OOP PHP

Posted 15 November 2011 - 08:01 PM

Look at this line:

  $objStudent = new Student($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests);


What is the value of $name?
What is the value of $hometown?
What is the value of $location?
... etc?
Was This Post Helpful? 1
  • +
  • -

#3 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 07:38 PM

They're referenced in the class file. What am I missing?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 08:04 PM

It's time to read up on using user defined functions.
Was This Post Helpful? 1
  • +
  • -

#5 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon


Reputation: 785
  • View blog
  • Posts: 2,965
  • Joined: 19-January 10

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 08:45 PM

It means your script has no idea what those variables are. Their values are not assigned anywhere.
Was This Post Helpful? 0
  • +
  • -

#6 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 08:50 PM

Aren't the variables being defined here?

 $objStudent = new Student($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests);
  $objStudent->name = $user_profile['name'];
  $objStudent->hometown = $user_profile['hometown']['id'];
  $objStudent->location = $user_profile['location']['id'];
  $objStudent->birthday = $user_profile['birthday'];
  $objStudent->collegeid = $user_profile['education'][1]['school']['id'];
  $objStudent->college = $user_profile['education'][1]['school']['name'];
  $objStudent->majorid = $user_profile['education'][1]['concentration'][0]['id'];
  $objStudent->major = $user_profile['education'][1]['concentration'][0]['name'];
  $objStudent->grad = $user_profile['education'][1]['year']['name'];
  $objStudent->company = $user_profile['work'][0]['employer']['name'];
  $objStudent->jobtitle = $user_profile['work'][0]['position']['name'];
  $objStudent->eduRecommender();
  ?>


I looked at the link but those are conditional functions which doesn't apply to this example. As I pointed out earlier, this is my first attempt at OOP so I'm a bit lost.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 09:00 PM

Functions are functions. Parameters are sent to them.
<?php
class example {
	public $test;
	
	function __construct($t) {
		$this->test = $t;
	}
}

$some_variable = "Some value.";
$myobj = new example($some_variable);
echo $myobj->test;
?>

Was This Post Helpful? 0
  • +
  • -

#8 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon


Reputation: 785
  • View blog
  • Posts: 2,965
  • Joined: 19-January 10

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 09:01 PM

What he means by that is take a look at your first line, non others. Do those variables have values? no.
Was This Post Helpful? 0
  • +
  • -

#9 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 09:16 PM

Alright so I tried this and finally got rid of the undefined errors:


 <?php

    require_once('class.Student.php');
    
    $name = $user_profile['name'];
    $hometown = $user_profile['hometown']['name'];
    $location = $user_profile['location']['name'];
    $birthday = $user_profile['birthday'];
    $collegeid = $user_profile['education'][1]['school']['id'];
    $college = $user_profile['education'][1]['school']['name'];
    $majorid = $user_profile['education'][1]['concentration'][0]['id'];
    $major = $user_profile['education'][1]['concentration'][0]['name'];
    $grad = $user_profile['education'][1]['year']['name'];
    $company = $user_profile['work'][0]['employer']['name'];
    $jobtitle = $user_profile['work'][0]['position']['name'];
    $interestsid = $interests['data'][0]['id'];
    $interests = $interests['data'][0]['name'];
    $interests1 = $interests['data'][1]['id'];
    $interests2 = $interests['data'][1]['name'];
    
    $objStudent = new Student($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests,$interests1,$interests2,$interestsid);   
    $objStudent->name = $user_profile['name'];
    $objStudent->hometown = $user_profile['hometown']['name'];
    $objStudent->location = $user_profile['location']['name'];
    $objStudent->birthday = $user_profile['birthday'];
    $objStudent->collegeid = $user_profile['education'][1]['school']['id'];
    $objStudent->college = $user_profile['education'][1]['school']['name'];
    $objStudent->majorid = $user_profile['education'][1]['concentration'][0]['id'];
    $objStudent->major = $user_profile['education'][1]['concentration'][0]['name'];
    $objStudent->grad = $user_profile['education'][1]['year']['name'];
    $objStudent->company = $user_profile['work'][0]['employer']['name'];
    $objStudent->jobtitle = $user_profile['work'][0]['position']['name'];
    $objStudent->interestsid = $interests['data'][0]['id'];
    $objStudent->interests = $interests['data'][0]['name'];
    $objStudent->interests1 = $interests['data'][1]['id'];
    $objStudent->interests2 = $interests['data'][1]['name'];
    $objStudent->goalRecommender();
    
    ?>


And now I'm getting this error for the first time:

Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\OOP\teststudent.php on line 73
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2484
  • View blog
  • Posts: 8,517
  • Joined: 08-August 08

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 09:20 PM

Delete lines 22 - 36.

I see only 39 lines of code.

Oh, you've got an include.
Delete the line numbers shown in your above code.
Was This Post Helpful? 0
  • +
  • -

#11 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 09:30 PM

Error message still there. Thanks for the help, though it's really appreciated.
Was This Post Helpful? 0
  • +
  • -

#12 creativecoding  Icon User is online

  • Hash != Encryption
  • member icon


Reputation: 785
  • View blog
  • Posts: 2,965
  • Joined: 19-January 10

Re: Undefined variable in OOP PHP

Posted 16 November 2011 - 09:39 PM

The error is on line 79, we have no idea what's on that line. How are we supposed to fix it without seeing the code?
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,505
  • Joined: 23-August 08

Re: Undefined variable in OOP PHP

Posted 17 November 2011 - 04:57 AM

Because there's an include, the line numbers are off a bit.

It's very obvious you're not getting the big picture here. This code:

$objStudent = new Student($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests,$interests1,$interests2,$interestsid);   
   $objStudent->name = $user_profile['name'];
   $objStudent->hometown = $user_profile['hometown']['name'];
   $objStudent->location = $user_profile['location']['name'];
   $objStudent->birthday = $user_profile['birthday'];
   $objStudent->collegeid = $user_profile['education'][1]['school']['id'];
   $objStudent->college = $user_profile['education'][1]['school']['name'];
   $objStudent->majorid = $user_profile['education'][1]['concentration'][0]['id'];
   $objStudent->major = $user_profile['education'][1]['concentration'][0]['name'];
   $objStudent->grad = $user_profile['education'][1]['year']['name'];
   $objStudent->company = $user_profile['work'][0]['employer']['name'];
   $objStudent->jobtitle = $user_profile['work'][0]['position']['name'];
   $objStudent->interestsid = $interests['data'][0]['id'];
   $objStudent->interests = $interests['data'][0]['name'];
   $objStudent->interests1 = $interests['data'][1]['id'];
   $objStudent->interests2 = $interests['data'][1]['name'];


Everything after the object creation is superfluous. The whole point of you passing all those parameters to the constructor is so you don't HAVE to assign them individually outside of the class. In fact, you could improve the code significantly by having the constructor take two arguments, the $user_profile and $interests arrays and using those in the constructor instead of going through the machinations of assigning the values in those arrays to new variables and passing them to the object's constructor.

And I think we need to know how you're building those two arrays as well.
Was This Post Helpful? 1
  • +
  • -

#14 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Undefined variable in OOP PHP

Posted 18 November 2011 - 10:41 AM

Thank you for the patience. I will explain what I am trying to do in more detail. I'm using the Facebook Graph API to build user profiles using OOP, or at least I'm trying. When you call the Graph API, user attribute values are passed on as arrays. For example: $user_profile['name'];

The problem is that not all users have all of these attribute values, most of the time many of them are blank. In this example, I'm trying to group several user attribute values into one class so I can access them as needed. I am trying to build a recommender system based on FB profile data. Here is the revised code implementing the notes from above. I have also included the calling of the graph API:

<?php
  require_once('php-sdk/facebook.php');

  $config = array(
    'appId' => 'XXXXXXXXXXXXXXXXX',
    'secret' => 'XXXXXXXXXXXXXXXXXXX',
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
?>
 <?php
    if($user_id) {

      try {

        $user_profile = $facebook->api('/me','GET');
       

      } catch(FacebookApiException $e) {
        $login_url = $facebook->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {
        
      $login_url = $facebook->getLoginUrl();
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>
  <?php
    if($user_id) {
      try {
            $interests = $facebook->api("/me/interests");
        }
        catch(Exception $o){
            d($o);
        }
    }
    ?>
    
    <?php

    require_once('class.Student.php');
    
    $name = $user_profile['name'];
    $hometown = $user_profile['hometown']['name'];
    $location = $user_profile['location']['name'];
    $birthday = $user_profile['birthday'];
    $collegeid = $user_profile['education'][1]['school']['id'];
    $college = $user_profile['education'][1]['school']['name'];
    $majorid = $user_profile['education'][1]['concentration'][0]['id'];
    $major = $user_profile['education'][1]['concentration'][0]['name'];
    $grad = $user_profile['education'][1]['year']['name'];
    $company = $user_profile['work'][0]['employer']['name'];
    $jobtitle = $user_profile['work'][0]['position']['name'];
    $interestsid = $interests['data'][0]['id'];
    $interests = $interests['data'][0]['name'];
    $interests1 = $interests['data'][1]['id'];
    $interests2 = $interests['data'][1]['name'];
    
    $objStudent = new Student($name,$hometown,$location,$birthday,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$interests,$interests1,$interests2,$interestsid);   
    $objStudent->goalRecommender();
    
    ?>


The error message I'm getting is: Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\OOP\teststudent.php on line 63

But I've access this variable in other scripts such as:

if ($interests['data'][0]['id']='104076956295773' && $interests['data'][1]['id']='103776309660881') {
                  echo "<p>We recommend working at Facebook as a UI designer.</p>";
                }


But for some reason I can't access these variables in this script. I appreciate all of the help I really am trying my best to solve these problems independently but I am obviously struggling.
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5662
  • View blog
  • Posts: 22,505
  • Joined: 23-August 08

Re: Undefined variable in OOP PHP

Posted 18 November 2011 - 05:57 PM

Ah, here's your problem:

$interests = $interests['data'][0]['name'];


You're redefining the $interests variable so now when you get to the next two lines:

$interests1 = $interests['data'][1]['id'];
$interests2 = $interests['data'][1]['name'];


instead of containing the data you think, it now contains only the contents of the original $interests['data'][0]['name'].
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1