繁体   English   中英

带FB Graph API的OOP PHP

[英]OOP PHP with FB Graph API

我正在通过Facebook Graph API在PHP中使用OOP创建Student对象。 我的问题是,并非所有用户在FB上共享相同数量的数据,因此,如果特定用户未列出对象中实例化的某些变量,则会收到未定义的变量消息。 为此做准备的最佳方法是什么,即创建一个对象,无论用户是否共享了该对象中的所有数据? 代码如下:

<?php
require_once('class.Student.php');

$name = $user_profile['name'];
$hometown = $user_profile['hometown']['name'];
$location = $user_profile['location']['name'];
$birthday = $user_profile['birthday'];
$highschool = $user_profile['education'][0]['school']['name'];
$hsgrad = $user_profile['education'][0]['year']['name'];
$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'];
$startdate = $user_profile['work'][0]['start_date'];
$interest = $interests['data'][0]['name'];
$interestid = $interests['data'][0]['id'];

$objStudent = new Student($name,$hometown,$location,$birthday,$highschool,$hsgrad,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$startdate,$interest,$interestid);   

?>

和类本身:

<?php

class Student {

public $name;
public $hometown;
public $location;
public $birthday;
public $highschool;
public $hsgrad;
public $collegeid;
public $college;
public $majorid;
public $major;
public $grad;
public $company;
public $jobtitle;
public $startdate;
public $interest;
public $interestid;


    public function __construct($name,$hometown,$location,$birthday,$highschool,$hsgrad,$collegeid,$college,$majorid,$major,$grad,$company,$jobtitle,$startdate,$interest,$interestid) {
        $this->name = $name;
        $this->hometown = $hometown;
        $this->location = $location;
        $this->birthday = $birthday;
        $this->highschool = $highschool;
        $this->hsgrad = $hsgrad;
        $this->collegeid = $collegeid;
        $this->college = $college;
        $this->majorid = $majorid;
        $this->major = $major;
        $this->grad = $grad;
        $this->company = $company;
        $this->jobtitle = $jobtitle;
        $this->startdate = $startdate;
        $this->interest = $interest;
        $this->interestid = $interestid;
    }

我了解如何使用简单的isset在类的函数中处理此问题,例如:

  function goalRecommender () {

        if (isset($this->interest)) {
            if ($this->interest =='Computer Science') {
            echo "<p>Based on your interest in Computer Science, we recommend working in the software industry. Furthermore, your interest in user interface design would be thoroughly put to use at Facebook, one of the fastest growing technology companies in the world. If you would like to pursue another goal,
            search our database to the right or click one of the 'popular goal' links.<p>";
            }
            elseif ($this->interests =='Finance') {
                echo "<p>You're interested in Finance</p>";
            }
            elseif ($this->interests =='Film') {
                echo "<p>You're interested in Film</p>";
        }
            elseif ($this->interests =='Marketing') {
                echo "<p>You're interested in Marketing</p>";
            } else {
                echo "<p>Choose a goal.</p>";
            }
        }   else  {
                echo "<p>What are you interested in?
                <select>
                <option>Finance</option>
                <option>Marketing</option>
                <option>Film</option>
                <option>Software</option>
                </select></p>";
        }
    } 

但是我只是在用PHP学习OOP,不确定在实例化对象时如何做。 任何指导将不胜感激。

您的课程很多:

  • 让您的Student构造函数接受一个数组。 这将减轻变量调整的痛苦,尤其是如果您最终扩展了Student 说明:您无需传递名称,大学,家乡,生日等信息,而是传递具有所有相同信息的数组。

     $name = $user_profile['name']; $hometown = $user_profile['hometown']['name']; //becomes $user['name'] = $user_profile['name']; $user['hometown'] = $user_profile['hometown']['name']; 
    • 它更快。 任何参数的成本都不是多少,但是当您拥有大量这样的参数时,它至少可以打分。
    • 打电话更容易。 您不再需要记住变量的顺序。 “是名字,家乡,位置,生日吗?还是名字,生日,位置,故乡?” 您有大量的变量,无法轻松记住这一点。
    • 更容易更改。 添加变量? 没问题,将其添加到数组中并在函数内部进行检查。 删除变量? 这很难做到,但是如果它在数组中就不行。

       public function __construct( $name, $hometown, $location, $birthday, $highschool, $hsgrad, $collegeid, $college, $majorid, $major, $grad, $company, $jobtitle, $startdate, $interest, $interestid) { //becomes public function __construct(array $info) { 
  • 您的变量应该是protectedprivate ,而不是public 这几乎是行业标准。
    • 您将要提供getter和setter方法,因为这些变量不可访问。 许多IDE都可以为您做到这一点。

至于您的错误消息:

  • 在实际访问变量之前,需要检查以确保该变量存在。 为此,请使用isset()函数:

     $hometown = isset($user_profile['hometown']['name']) ? $user_profile['hometown']['name'] : null; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM