Jump to content

syntax error, unexpected ')', expecting '('


osherdo
Go to solution Solved by requinix,

Recommended Posts

I have this code:

 

class DropzoneController extends Controller
{


  public function __construct()
  {


      $this->middleware('auth');
      if (!Auth::check()) { return redirect('auth/login'); }


      $this->user = Auth::user();


  }


  public function index()
{
  return view('dropzone_demo');
}


public function uploadFiles()
{
  $message="Profile Image Created";
  $input = Input::all();


  $rules = array(
      'file' => 'image|max:3000',
  );


  $validation = Validator::make($input, $rules);


  if ($validation->fails())
  {
      return Response::make($validation->errors->first(), 400);
  }


  $destinationPath = 'uploads'; // upload path: public/uploads
  $extension = Input::file('file')->getClientOriginalExtension(); // getting file extension
  $fileName = rand(11111, 99999) . '.' . $extension; // renameing image
  $upload_success = Input::file('file')->move($destinationPath, $fileName); // uploading file to given path


  if ($upload_success)
  {
      return Response::json('success', 200);
  } else
  {
      return Response::json('error', 400);
  }


//  attaching the profile image to the authenticated user.


$user->profileImage()->create([
    'filename' => $filename
]);


Session::flash('new_profile_image', 'Profile Photo Updated!');




}


public function authorize($ability, $arguments = Array)
    {
        // set to true instead of false
        return true;
    }
}

The error occurs for this line:

public function authorize($ability, $arguments = Array)

and it says:

 

syntax error, unexpected ')', expecting '('

Not sure what that means. Anyone who can tell me what the issue is?

Link to comment
Share on other sites

  • Solution

If you want to make sure $argument is an array then you need

public function authorize($ability, array $arguments)
If you want the default value to be an empty array then you need

public function authorize($ability, $arguments = array())
If you want both - which I suspect is the case - then do both.
Link to comment
Share on other sites

If you want to make sure $argument is an array then you need

public function authorize($ability, array $arguments)
If you want the default value to be an empty array then you need

public function authorize($ability, $arguments = array())
If you want both - which I suspect is the case - then do both.

 

It seems that only this works:

 

public function authorize($ability, $arguments = array())
 

 

because using the other one, or using them both as an arguments returns this error: (I am using Laravel, and it returns this)

 

Declaration of App\Http\Controllers\DropzoneController::authorize() should be compatible with App\Http\Controllers\Controller::authorize($ability, $arguments = Array)
 I am now getting an error of Laravel, so that'll do for now. much thanks!
Link to comment
Share on other sites

Yes it is now available to everyone. But that doesn't mean the original "array()" syntax is obsolete. Nor is it discouraged, nor is "[]" a best practice, nor is really anything true besides there being two array syntaxes available.

 

Frankly, I think [] gets lost in the mess of other symbols too easily. It's sure handy for writing an array but sometimes I like seeing the word "array" for clarity.

  • Like 1
Link to comment
Share on other sites

Sounds like you've tried “array[]”. That's not what I said. I'm talking about replacing “array()” with the new array literal “[]”. So instead of the keyword “array” followed by a pair of parentheses, it's now just a pair of square brackets without any “array” keyword, just like in JavaScript (or Ruby, Python, ...). If you're still not sure how the correct syntax looks like, read the manual.

 

 

 

Frankly, I think [] gets lost in the mess of other symbols too easily. It's sure handy for writing an array but sometimes I like seeing the word "array" for clarity.

 

You can like whatever you want. I'm sure there are also people who like to write $a{$i} instead of $a[$i], and that's fine as long as nobody bothers to officially deprecate or remove this syntax.

 

The point is that instead of the old, verbose PHPism “array(...)”, there's now a literal syntax which is compact, easy to read and consistent with other languages. The PSR documents use it (even though it's not officially endorsed), Laravel uses it, and just about every other big project has at least considered using it. The only reason why this hasn't always happened is because of legacy code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.