Skip to content

prefix-underscorefixable

Ensure that identifiers are not prefixed with an underscore

Magic methods and super global varaibles are excluded from this rule.

Options

exclude

Exclude certain kinds of identifiers from being checked

Type: array

Default: []

Possible values: class, interface, trait, enum, enumcase, property, method, variable, parameter, function, constant

Examples

  • Examples of correct code for this rule using default options

Class and properties with no prefixed underscore

php
<?php
class Database {
	private $max_connections = 1;
	public $min_requests = 1;
	
	public function connect($name) {
		echo "connected to $name";
	}
	
	private function disconnect($name) {
		echo "disconnected from $name";
	}
	
	public function getConnections() {
		return $this->max_connections;
	}
	
	public function setConnections($connections) {
		$this->max_connections = $connections;
	}
}

Trait, interface and enum with no prefixed underscore

php
<?php
trait ConnectSqlServerDatabaseToTrait {
	public function helpMeConnectSqlServerDatabases() {
		$SQLServerdatabaseConnectionString = '';
	}
}

interface ConnectSqlServerDatabaseToModel {
	public function youHaveToConnectSQLServerDatabase();
}

enum DatabasesTypesConnectingToServer {
	case SQL;
	case NoSQL;
	case Graph;
	case Key_Value;
}

Constants and variables with no prefixed underscore

php
<?php
const FOO_BAR = 1;
const FOO_BAR2 = 1;
const MAX_MEMORY = 1;
const MIN_MAX = 1;
const MAX_CONNECTIONS = 1;

$foo_bar = 1;
$foo_bar_baz = 1;
$max_memory = 1;
$min_max = 1;
$max_connections = 1;

functions and parameters with no prefixed underscore

php
<?php
function log_info($message, $level) {
	echo "INFO: $message";
}

function log_error($message, $level) {
	echo "ERROR: $message";
}

function log_warning($message, $level) {
	echo "WARNING: $message";
}
  • Examples of incorrect code for this rule using default options

Identifiers with prefixed underscore

php
<?php
class __Database {
	private $_max_connections = 1;
	public $min_requests = 1;
	
	public function connect($_name) {
		echo "connected to $_name";
	}
	
	private function _disconnect($name) {
		echo "disconnected from $name";
	}
	
	public function _getConnections() {
		return $this->_max_connections;
	}
	
	public function setConnections($_connections) {
		$this->_max_connections = $_connections;
	}
}

Trait, interface and enum with prefixed underscore

php
<?php
trait _ConnectSqlServerDatabaseToTrait {
	public function _helpMeConnectSqlServerDatabases() {
		$SQLServerdatabaseConnectionString = '';
	}
}

interface _ConnectSqlServerDatabaseToModel {
	public function _youHaveToConnectSQLServerDatabase();
}

enum DatabasesTypesConnectingToServer {
	case _SQL;
	case _NoSQL;
	case _Graph;
	case Key_Value;
}

exclude

  • Examples of correct code for this rule using exclude option

Properties and methods with prefixed underscore

php
<?php
/* taqwim "psr/prefix-underscore" : {exclude: ['property', 'method']} */
class Database {
	private $_max_connections = 1;
	public $min_requests = 1;
	
	public function connect($name) {
		echo "connected to $name";
	}
	
	private function _disconnect($name) {
		echo "disconnected from $name";
	}
	
	public function _getConnections() {
		return $this->_max_connections;
	}
	
	public function setConnections($connections) {
		$this->_max_connections = $connections;
	}
}

Released under the MIT License.