agi*_*gis 2 php url conditional if-statement
我想在我的代码中包含这个if条件:
if(basename($_SERVER['REQUEST_URI']) == 'demo'){
// Do something
}
我只想在这个条件成立时触发我的代码的一部分,所以我的整个代码看起来像这样:
<?php
abstract class ECF_Field_Type {
private static $types = array();
protected $name;
/* Constructor */
public function __construct() {
self::register_type( $this->name, $this );
}
/* Add a type to the types list */
private static function register_type( $name, $type ) {
self::$types[$name] = $type;
}
/* Return the appropriate type */
public static function get_type( $name ) {
return array_key_exists( $name, self::$types )
? self::$types[$name] : self::get_default_type();
}
/* Return all types */
public static function get_types() {
return self::$types;
}
/* Get default type */
public static function get_default_type() {
return self::get_type( 'text' );
}
/* Display form field */
public abstract function form_field( $name, $field );
if(basename($_SERVER['REQUEST_URI'])) == 'demo'){
public function display_field( $id, $name, $value ) {
return "<span class='ecf-field ecf-field-$id'>"
. "<strong class='ecf-question'>$name:</strong>"
. " <span class='ecf-answer'>$value</span></span>\n";
}
}
/* Display field plain text suitable for email display */
public function display_plaintext_field( $name, $value ) {
return "$name: $value";
}
/* Get the description */
abstract public function get_description();
}
?>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中检查这个时,我得到了
Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in
if(basename($_SERVER['REQUEST_URI']) == 'demo'){
如果我把这个if(basename($_SERVER['REQUEST_URI']) == 'demo'){ // Do something }
外面abstract class ECF_Field_Type {
的条件工作正常.
我怎样才能让这个if
条件在里面工作abstract class
?
我只是希望能够public function display_field( $id, $name, $value )
在条件成立时运行它.
您不能在类的"外部"方法中使用代码.例如
class foo {
if (true) { ... }; <---bad code
function bar() {
if (true) { ... } <-- ok code
}
}
Run Code Online (Sandbox Code Playgroud)