NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Create Get{VariableName} and Set{VariableName} functions


Joined: 23 Jun 2008
Posts: 32
Reply with quote
Here is my version of creating get and set functions to classes.

Sample
Code before:
Code:
<?php
class Sample {
  var $Foo;

  public $Bar;

  private $Foobar;
}
?>


Code after:
Code:
<?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:
Code:
<?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
      - for files

    - Show this command in Tools menu
    - Show this command in File Bar popup
    - Work with editor
      - Take input from
        - Whole file

    - Redirect error stream to log window


Hope you find this script useful!
View user's profileFind all posts by AirspraySend private message
Re: Create Get{VariableName} and Set{VariableName} functions


Joined: 18 Feb 2010
Posts: 4
Location: Switzerland
Reply with quote
AND

5. Options -> Work with Editor -> Return results to editor

Insert (vs Replace) is up to you

Airspray wrote:
Here is my version of creating get and set functions to classes.


Hope you find this script useful!


Great! Thanks Airspray
View user's profileFind all posts by ILavignySend private message


Joined: 23 Jun 2008
Posts: 32
Reply with quote
I have updated the script to add default values for all member variables to end of __construct(). This version also adds PhPDocumentor comments for variables/functions.

If you don't like PhpDocumentor comments, they can easily be commented out from code. I've added comments like "// Comment this line out..." before those three lines.

Code:
<?php
/**
* (C) 2010 Niklas Lampén
*/
class CreateGetSet
{
    private $Text;

    public function __construct()
    {
        // Read file contents
        $this->Text = file_get_contents('php://stdin');
    }
   
    private function UpdateConstructor($MemberVariable)
    {
        /**
        * Find full __construct() from source. If it's present, add
        * "$this->MemberVariable = null;" to end of it.
        */
        if (preg_match('/function __construct\(.*\)\s*\{((?:\{.*\}|.*)*)\}/sUmi', $this->Text, $aMatches))
        {
            // Check if member variable is allready inited in constructor
            if (!preg_match('/\$this->' . $MemberVariable . '\s*=\s*/i', $aMatches[1]))
            {
                $this->Text = preg_replace('/(function __construct\(.*\)\s*\{((?:\{.*\}|.*)*))\}/sUmi',
                    "$1\t\$this->" . $MemberVariable . " = null;\n\t}", $this->Text);
            }
        }
    }

    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"
                   
                    // Comment this line out if you don't want to get PhpDocumentor comments
                    . "\t/**\n" . $aMatches[1][$i] . '* Get value of ' . $MemberVariable . "\n\t* \n\t * @return mixed\n\t*/\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 . ";/",
                   
                    // Comment this line out if you don't want to get PhpDocumentor comments
                    "/**\n" . $aMatches[1][$i] . '* Description of variable ' . $MemberVariable . "\n\t* \n\t* @var mixed\n\t*/\n" . $aMatches[1][$i] .
                   
                    $aMatches[2][$i] . " \$$MemberVariable;\n"
                   
                    // Comment this line out if you don't want to get PhpDocumentor comments
                    . "\t/**\n" . $aMatches[1][$i] . '* Set value of ' . $MemberVariable . "\n\t* \n\t* @param mixed $$MemberVariable\n\t*/\n"
                   
                    . $aMatches[1][$i] . 'public function Set' . $MemberVariable . '($' . $MemberVariable . ') { $this->' . $MemberVariable . ' = $' . $MemberVariable . "; }",
                    $this->Text);
            }
           
            $this->UpdateConstructor($MemberVariable);
        }
       
        return trim($this->Text);
    }
}

$GetSet = new CreateGetSet();
echo $GetSet->GetContent();
?>
View user's profileFind all posts by AirspraySend private message
Create Get{VariableName} and Set{VariableName} functions
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic