How to access protected method?

I have a question.

This is my model.




<?php


/**

 * This is the model class for table "tbl_user".

 *

 * The followings are the available columns in table 'tbl_user':

 * @property integer $id

 * @property string $username

 * @property string $password

 * @property string $salt

 * @property string $email

 * @property string $profile

 */

class User extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return User the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'tbl_user';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, salt, email', 'required'),

			array('username, password, salt, email', 'length', 'max'=>128),

			array('profile', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, username, password, salt, email, profile', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'username' => 'Username',

			'password' => 'Password',

			'salt' => 'Salt',

			'email' => 'Email',

			'profile' => 'Profile',

		);

	}


    /**

	 * Checks if the given password is correct.

	 * @param string the password to be validated

	 * @return boolean whether the password is valid

	 */

	public function validatePassword($password)

	{

		return $this->hashPassword($password,$this->salt)===$this->password;

	}


	/**

	 * Generates the password hash.

	 * @param string password

	 * @param string salt

	 * @return string hash

	 */

	public function hashPassword($password,$salt)

	{

		return md5($salt.$password);

	}


	/**

	 * Generates a salt that can be used to generate a password hash.

	 * @return string the salt

	 */

	protected function generateSalt()

	{

		return uniqid('',true);

	}

}

This is part of my controller


//--------------------------------------------------------------------------------------

    public function actionRegister() {

               

                $model=new RegisterForm;

                $newUser = new User;

                

                // if it is ajax validation request

                if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

                {

                        echo CActiveForm::validate($model);

                        Yii::app()->end();

                }


                // collect user input data

                if(isset($_POST['RegisterForm']))

                {

                        $model->attributes=$_POST['RegisterForm'];

                        

                     //   if ($model->validate()) {

                                $newUser->username = $model->username;

                                $newSalt = $newUser->generateSalt();

                                

                                $newUser->password = $newUser->hashPassword($model->password,$newSalt);

                                $newUser->salt = $newSalt;


                                //return $this->hashPassword($password,$this->salt)===$this->password;

                                $newUser->username = $model->username;


                                $newUser->email = $model->email;

                                $newUser->joined = date('Y-m-d');

                                        

                                if ($model->validate() && $newUser->save()) {

                                        $identity=new UserIdentity($newUser->username,$model->password);

                                        $identity->authenticate();

                                        Yii::app()->user->login($identity,0);

                                        //redirect the user to page he/she came from

                                        $this->redirect(Yii::app()->user->returnUrl);

                                }

                      //  }

                                

                }

                // display the register form

                $this->render('Register',array('model'=>$model));

        }  

//--------------------------------------------------------------------------------------

This code $newSalt = $newUser->generateSalt(); return me error "User and its behaviors do not have a method or closure named "generateSalt".".

Then how actually to access this method?

Hello, protected methods can only be accessed and used within the class itself.

that is why it is called protected.

if you are sure its not a protected method

you can try




$newSalt=$newUser->generateSalt; 



Thank you for fast reply.

But sorry, still cannot access the method. I need to generate the salt to combine with the password.

Can i change it to public or can i move it into my SiteController? is it dangerous? I’m using Gii creating this User model and controller, its default setting is protected, i dont know why.

If you need to access to a method why is protected?

So, … you can create a public method that uses that protected method.