Here is my version of creating get and set functions to classes.
Sample
Code before:
<?php
class Sample {
var $Foo;
public $Bar;
private $Foobar;
}
?> |
Code after:
<?php
class Sample {
var $Foo;
public function SetFoo($Foo) { $this->Foo = $Foo; }
public function GetFoo() { return $this->Foo; }
public $Bar;
public function SetBar($Bar) { $this->Bar = $Bar; }
public function GetBar() { return $this->Bar; }
private $Foobar;
public function SetFoobar($Foobar) { $this->Foobar = $Foobar; }
public function GetFoobar() { return $this->Foobar; }
}
?> |
Install script
1. Create file
c:\PhpEd Scripts\createGetSet.php with this content:
<?php
/**
* (C) 2010 Niklas Lampén
*/
class CreateGetSet
{
private $Text;
public function __construct()
{
// Read file contents
$this->Text = file_get_contents('php://stdin');
}
public function GetContent()
{
/**
* Match "{definition} ${VariableName};"
*/
preg_match_all("/(\t*| *)(var|private|public|protected)\s+\\$([a-z0-9]+);/i", $this->Text, $aMatches);
foreach ($aMatches[3] as $i => $MemberVariable)
{
/**
* If function Get{VariableName} is not found, create it
*/
if (preg_match("/function Get" . $MemberVariable. "\(/i", $this->Text) == 0)
{
$this->Text = preg_replace(
"/" . $aMatches[2][$i] . ' \\$' . $MemberVariable . ";/",
$aMatches[2][$i] . " \$$MemberVariable;\n" . $aMatches[1][$i] . 'public function Get' . $MemberVariable . '() { return $this->' . $MemberVariable . "; }",
$this->Text);
}
/**
* If function Set{VariableName} is not found, create it
*/
if (preg_match("/function Set" . $MemberVariable. "\(/i", $this->Text) == 0)
{
$this->Text = preg_replace(
"/" . $aMatches[2][$i] . ' \\$' . $MemberVariable . ";/",
$aMatches[2][$i] . " \$$MemberVariable;\n" . $aMatches[1][$i] . 'public function Set' . $MemberVariable . '($' . $MemberVariable . ') { $this->' . $MemberVariable . ' = $' . $MemberVariable . "; }",
$this->Text);
}
}
return trim($this->Text);
}
}
$GetSet = new CreateGetSet();
echo $GetSet->GetContent();
?> |
2. Open
Tools > Settings > Tools > Integration
3. Click on
Add Menu and name it "
Create GetSet", click
OK and then click
Edit
4. Choose Execute with
Shell and set command line to '
@PHP5@ -f "c:\PhpEd Scripts\createGetSet.php"' (without single quotes)
5. Check following options:
- Filter (set value to "*.php;*.inc")
- Show this command in Workspace popup
- Show this command in Tools menu
- Show this command in File Bar popup
- Work with editor
- Redirect error stream to log window
Hope you find this script useful!