致命错误:在非对象上调用成员函数getObject()

时间:2012-01-23 13:52:04

标签: php mysql fatal-error

我正在使用这本名为PHP5社交网络的社交网站。 我试图编辑个人资料但我收到此错误

致命错误:在非对象上调用成员函数getObject(),该对象引用以下行;

if ( $registry->getObject('authenticate')->isLoggedIn() 
 && ( $registry->getObject('authenticate')->getUser()->getUserID() == $this->id 
   || $registry->getObject('authenticate')->getUser()->isAdmin() == true ) )

我是PHP的新手,所以任何帮助都会受到极大的赞赏。

这是整个文件;

<?php

/**
 * Profile model
 */
class Profile{

    /**
     * The registry
     */
    private $registry;

    /**
     * Profile ID
     */
    private $id;

    /**
     * Fields which can be saved by the save() method
     */
    private $savable_profile_fields = array( 'name', 'hometown', 'university', 'gender', 'photo', 'bio', 'dob' );

    /**
     * Users ID
     */
    private $user_id;

    /**
     * Users name
     */
    private $name;

    /**
     * Users hometown
     */
    private $hometown;

    /**
     * Users University 
     */
    private $university;

    /**
     * Users genders
     */
    private $gender;

    /**
     * Users bio
     */
    private $bio;

    /**
     * Users dob
     */
    private $dob;

    /**
     * Users photograph
     */
    private $photo;

    private $valid;

    /**
     * Profile constructor
     * @param Registry $registry the registry
     * @param int $id the profile ID
     * @return void
     */
    public function __construct( Registry $registry, $id=0 )
    {
        $this->registry = $registry;
        if( $id != 0 )
        {
            $this->id = $id;
            // if an ID is passed, populate based off that
            $sql = "SELECT * FROM profile WHERE user_id=" . $this->id;
            $this->registry->getObject('db')->executeQuery( $sql );
            if( $this->registry->getObject('db')->numRows() == 1 )
            {
                $this->valid = true;

                $data = $this->registry->getObject('db')->getRows();
                // populate our fields
                foreach( $data as $key => $value )
                {
                    $this->$key = $value;
                }
            }
            else
            {
                $this->valid = false;
            }

        }
        else
        {
            $this->valid = false;
        }
    }

    /**
     * Is the profile valid
     * @return bool
     */
    public function isValid()
    {
        return $this->valid;
    }

    /**
     * Sets the users name
     * @param String $name
     * @return void
     */
    public function setName( $name )
    {
        $this->name = $name;
    }


    /** 
    * Sets the users university 
    $@param String $university 
    */
    public function setUniversity( $university )
    {
        $this->university = $university;

    }


    /**
     * Set the users date of birth
     * @param String $dob the date of birth
     * @param boolean $formatted - indicates if the controller has formatted the dob, or if we need to do it here
     */
    public function setDOB( $dob, $formatted=true )
    {
        if( $formatted == true )
        {
            $this->dob = $dob;
        }
        else
        {
            $temp = explode('/', $dob );
            $this->dob = $temp[2].'-'.$temp[1].'-'.$temp[0];
        }
    }

    /**
     * Sets the hometown of users
     * @param String $hometown
     * return void
     */
    public function setHometown( $hometown )
    {
        $this->hometown = $hometown;
    }

    /**
     * Set the gender of the user
     * @param String $gender the gender
     * @param boolean $checked - indicates if the controller has validated the gender, or if we need to do it
     * @return void
     */
    public function setGender( $gender, $checked=true )
    {
        if( $checked == true )
        {
            $this->gender = $gender;
        }
        else
        {
            $genders = array();
            if( in_array( $gender, $genders ) )
            {
                $this->gender = $gender;
            }
        }
    }

    /**
     * Sets the users bio
     * @param String bio
     * @return void
     */
    public function setBio( $bio )
    {
        $this->bio = $bio;
    }

    /**
     * Sets the users profile picture
     * @param String photo name
     * @return void
     */
    public function setPhoto( $photo )
    {
        $this->photo = $photo;
    }

    /**
     * Save the user profile
     * @return bool
     */
    public function save()
    {
        // handle the updating of a profile
        if( $registry->getObject('authenticate')->isLoggedIn() && ( $registry->getObject('authenticate')->getUser()->getUserID() ==  $this->id || $registry->getObject('authenticate')->getUser()->isAdmin() == true  ) )


        {
            // we are either the user whose profile this is, or we are the administrator
            $changes = array();
            foreach( $this->saveable_profile_fields as $field )
            {
                $changes[ $field ] = $this->$field;
            }
            $this->registry->getObject('db')->updateRecords( 'profile', $changes, 'user_id=' . $this->id );
            if( $this->registry->getObject('db')->affectedRows() == 1 )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * Convert the users profile data to template tags
     * @param String $prefix prefix for the template tags
     * @return void
     */
    public function toTags( $prefix='' )
    {
        foreach( $this as $field => $data )
        {
            if( ! is_object( $data ) && ! is_array( $data ) )
            {
                $this->registry->getObject('template')->getPage()->addTag( $prefix.$field, $data );
            }
        }
    }



    /**
     * Return the users data
     * @return array
     */
    public function toArray( $prefix='' )
    {
        $r = array();
        foreach( $this as $field => $data )
        {
            if( ! is_object( $data ) && ! is_array( $data ) )
            {
                $r[ $field ] = $data;
            }
        }
        return $r;
    }

    /**
     * Get the users name
     * @return String
     */
    public function getName()
    {
        return $this->name;
    }


    /**
     * Get the users photograph
     * @return String
     */
    public function getPhoto()
    {
        return $this->photo;
    }

    /**
     * Get the users ID
     * @return int
     */
    public function getID()
    {
        return $this->user_id;
    }

}


?>

It also needs to pass through;
profileinformationcontroller.php

<?php

/**
 * Profile information controller
 */
class Profileinformationcontroller {

    /**
     * Constructor
     * @param Registry $registry
     * @param int $user the user id
     * @return void
     */
    public function __construct( $registry, $directCall=true, $user )
    {
        $this->registry = $registry;
        $urlBits = $this->registry->getObject('url')->getURLBits();
        if( isset( $urlBits[3] ) )
        {
            switch( $urlBits[3] )
            {
                case 'edit':
                    $this->editProfile();
                    break;
                default:
                    $this->viewProfile( $user );
                    break;
            }   
        }
        else
        {
            $this->viewProfile( $user );
        }

    }

    /**
     * View a users profile information 
     * @param int $user the user id
     * @return void
     */
    private function viewProfile( $user )
    {
        // load the template
        $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'profile/information/view.tpl.php', 'footer.tpl.php' );
        // get all the profile information, and send it to the template
        require_once( FRAMEWORK_PATH . 'models/profile.php' );
        $profile = new Profile( $this->registry, $user );
        $profile->toTags( 'p_' ); 
    }

/**
 * Edit your profile
 * @return void
 */
private function editProfile()
{
    if( $this->registry->getObject('authenticate')->isLoggedIn() == true )
    {
        $user = $this->registry->getObject('authenticate')->getUser()->getUserID();
        if( isset( $_POST ) && count( $_POST ) > 0 )
        {
                    require_once( FRAMEWORK_PATH . 'models/profile.php' );

            // edit form submitted
            $profile = new Profile( $this->registry, $user );
            $profile->setName( $this->registry->getObject('db')->sanitizeData( $_POST['name'] ) );
            $profile->setHometown( $this->registry->getObject('db')->sanitizeData( $_POST['hometown'] ) );
            $profile->setUniversity( $this->registry->getObject('db')->sanitizeData( $_POST['university'] ) );
            $profile->setGender( $this->registry->getObject('db')->sanitizeData( $_POST['gender'] ), false );
            $profile->setBio( $this->registry->getObject('db')->sanitizeData( $_POST['bio'] ) );
            $profile->setDOB( $this->registry->getObject('db')->sanitizeData( $_POST['dob'] ), false );
            if( isset( $_POST['profile_picture'] ) )
            {
                require_once( FRAMEWORK_PATH . 'lib/images/imagemanager.class.php' );
                $im = new Imagemanager();
                $im->loadFromPost( 'profile_picture', $this->registry->getSetting('uploads_path') .'profile/', time() );
                if( $im == true )
                {
                    $im->resizeScaleHeight( 150 );
                    $im->save( $this->registry->getSetting('uploads_path') .'profile/' . $im->getName() );
                    $profile->setPhoto( $im->getName() );
                }
            }
            $profile->save();
            $this->registry->redirectUser( array('profile', 'view', 'edit' ), 'Profile saved', 'The changes to your profile have been saved', false );
        }
        else
        {
            // show the edit form
            $this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'profile/information/edit.tpl.php', 'footer.tpl.php' );
            // get the profile information to pre-populate the form fields
            require_once( FRAMEWORK_PATH . 'models/profile.php' );
            $profile = new Profile( $this->registry, $user );
            $profile->toTags( 'p_' ); 
        }
    }
    else
    {
        $this->registry->errorPage('Please login', 'You need to be logged in to edit your profile');
    }
}

}


?>

有没有人可以提供帮助? 如果它有帮助,我会将源代码发送到您的电子邮件中吗?

提前致谢:)

1 个答案:

答案 0 :(得分:2)

您没有引用要访问的对象属性$registry,而是未定义的局部变量$registry。将行更改为:

if( $this->registry->getObject('authenticate')->isLoggedIn() && ( $this->registry->getObject('authenticate')->getUser()->getUserID() ==  $this->id || $this->registry->getObject('authenticate')->getUser()->isAdmin() == true  ) )

... 将此行添加到现有行之上:

$registry = $this->registry;

我会选择第一个选项,但实际上它没什么区别。