简体   繁体   中英

Random syntactical error in my php code that I can't find

Ordinarily I hate coming here with newbie code questions but nobody can find the error with this code. Maybe you guys can :-)

<?php
defined('SYSPATH') or die('No direct script access.');

/**
 * to interact with photos
 *
 * @author Max Padraig Wolfgang Bucknell-Leahy
 */
class Model_Photos
{
    private $apiKey = '12664498208a1380fe49fb1b5a238ef0';
    private $secret = '03d43dee65a34513';
    private $perms  = 'read';
    private $sigString = 'test';
    private $apiSig = md5($_sigString); //Line 15
    private $authArray = array('api_key' => $apiKey,
                               'perms'  => $perms,
                               'api_sig' => $apiSig);
    private $authArrayImploded = implode('&', $authArray);
    private $authLink = 'http://www.flickr.com/services/auth/?' . $authArrayImploded;

    public function get_photos($number = 5)
    {
        if(file_exists(APPPATH . 'cache/main_cache.xml')
        {
            echo $authLink;
        } else {
            echo 'not so good';
        }
    }
}

$class = new Model_Photos;

$class->get_photos;

the error is:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/p14s9nnd/public_html/testing.php on line 15

Thank you in advance and sorry

Regards, Max

private $apiSig = md5($_sigString);

You can't use functions/methods when declaring class properties. This should be the cause of your error but as others are pointing out, there are several issues with this code that will keep it from executing.

if(file_exists(APPPATH . 'cache/main_cache.xml')

缺少右括号?

I don't think you can use functions or variables when defining class members in PHP.

So this lines here are wrong:

private $apiSig = md5($_sigString);
'api_key' => $apiKey,
'perms'  => $perms,
'api_sig' => $apiSig
private $authArrayImploded = implode('&', $authArray);
private $authLink = 'http://www.flickr.com/services/auth/?' . $authArrayImploded;

Have a look here: http://ch.php.net/manual/en/language.oop5.properties.php

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Mike B has the first correct answer for the first parse error, but these lines aren't going to work either:

// this array declaration won't work because you can't reference variables
// ($apiKey, $perms, $apiSig) in a class declaration.
private $authArray = array('api_key' => $apiKey,
  'perms'  => $perms,
  'api_sig' => $apiSig);

// you can't call functions in class declaration
private $authArrayImploded = implode('&', $authArray);

// you can't use the '.' operator (or any other operator) here.
private $authLink = 'http://www.flickr.com/services/auth/?' . $authArrayImploded;

You should be initializing all these values in the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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