Kernel word recovery crack, Mobile themes website for samsung wave 525, Dekha ek khwab sony tv title song, Song zombies on your lawn, Bloody mary nerve endings mp3, Digittrade tv jukebox, Padosan songs mp3, Gold cup series 70 manual, Nieuwste versie van mozilla firefox en, Cant install spotify on mac, Web brute force password cracker, Belkin g peek driver windows 7

Generate PHP accessors

July 29th, 2009 by Martijn Leave a reply »

I’m working on a PHP project right now, and finally got tired of writing the same code over and over again (and a bit nostalgic of Perl, my first love), so here is gen_php_accessors.

#!/usr/bin/perl
# Martijn van der Kwast 2009
# Use and distribute freely
 
use Getopt::Std;
 
$indent = '    ';
$indent2 = $indent x 2;
 
if ( ! getopts('csGatrh') || $opt_h ) {
	print STDERR <<HELP;
gen_php_accessors [-c] [-s] [-G] [-a] [-t] [-h] < variable_declarations.php
Generate PHP accessors from member variables.
 -c Also generate constructor
 -s Also generate setters
 -G Don't generate getters
 -a Generate getters, setters, static getters and constructor
 -t Generate static getters
 -r Generate static setters
 -h Display this help
HELP
	exit -1;
}
 
$opt_s = 1 if $opt_a;
$opt_c = 1 if $opt_a;
$opt_t = 1 if $opt_a;
 
$visibility = '(?:(?:var|public|private|protected)\s+)';
$identifier = '[a-z_]\w*';
 
while(<>) {
	if ( /^\s*(?:static\s+)${visibility}?\$($identifier)\s*[;=]/i ||
	     /^\s*${visibility}?(?:static\s+)\$($identifier)\s*[;=]/i )
	{
		push @static, $1
	}
	elsif ( /^\s*${visibility}?\$($identifier)\s*[;=]/i ) {
		push @fields, $1 
	}
	print;
}
 
if ( $opt_c ) {
	print "\n";
	if ( @fields ) {
		$args = join(', ', map { "\$$_" } @fields );
		print "${indent}public function __construct( $args ) {\n";
		print "${indent2}\$this->$_ = \$$_;\n"
			for @fields;
		print "${indent}}\n";
	}
	else {
		print "${indent}public __construct() {  }\n";
	}
}
 
if ( @fields ) {
	if ( ! $opt_G ) {
		print "\n";
		print "${indent}public function $_() { return \$this->$_; }\n"
			for @fields;
	}
	if ( $opt_s ) {
		print "\n";
		print "${indent}public function set_$_( \$$_ ) { \$this->$_ = \$$_; }\n"
			for @fields;
	}
}
 
if ( @static && $opt_t ) {
	if ( $opt_t ) {
		print "\n";
		print "${indent}public static function $_() { return self::\$$_; }\n"
			for @static;
	}
	if ( $opt_r ) {
		print "\n";
		print "${indent}public static function set_$_( $_ ) { self::\$$_ = $_; }\n"
			for @static;
	}
}

Put it in a directory in $PATH, chmod +x it, and use it as a filter on the member variables you just selected. (vim: '<,'>!gen_php_accessors -a ).

You may want to adapt a little to match your PHP coding style (indentation, function names).

(modified to include constructors and static members)

Advertisement

Leave a Reply

Authorized tags:
[b]bold[/b] [u]underline[/u] [i]italic[/i] [url=http://link.url]link[/url] more