Altering Existing Functions
So let's first completely overwrite a function. Let's first define a function that does something simple.
function capFirstLetter(%string)
{
if(%string !$= "")
return strUpr(getSubStr(%string,0,1)) @ getSubStr(%string,1,strLen(%string));
}
So now we have our defined function, let's create a package and overwrite it.
package overwritePackage
{
function capFirstLetter(%string)
{
//Our capFirstLetter function will now be overwritten
//and do nothing now
}
};
activatePackage(overwritePackage);
So if we call our function capFirstLetter, it will not do anything at all. We can reverse this by simply using the deactivatePackage(overwritePackage); function and it'll work like we never messed with it at all!
function capFirstLetter(%string)
{
if(%string !$= "")
return strUpr(getSubStr(%string,0,1)) @ getSubStr(%string,1,strLen(%string));
}
So now we have our defined function, let's create a package and overwrite it.
package overwritePackage
{
function capFirstLetter(%string)
{
//Our capFirstLetter function will now be overwritten
//and do nothing now
}
};
activatePackage(overwritePackage);
So if we call our function capFirstLetter, it will not do anything at all. We can reverse this by simply using the deactivatePackage(overwritePackage); function and it'll work like we never messed with it at all!
Let's add some code to be ran before the function's original code.
package overwritePackage
{
function capFirstLetter(%string)
{
echo("This is happening first, before the original functionality.");
return parent::capFirstLetter(%string);
}
};
activatePackage(overwritePackage);
Notice the parent namespace. This is critical and when called will run the original code of our capFirstLetter function.
It also returns what the original function would return so be sure to either store it to a variable and return that variable later on, or simply return the parent call itself.
package overwritePackage
{
function capFirstLetter(%string)
{
echo("This is happening first, before the original functionality.");
return parent::capFirstLetter(%string);
}
};
activatePackage(overwritePackage);
Notice the parent namespace. This is critical and when called will run the original code of our capFirstLetter function.
It also returns what the original function would return so be sure to either store it to a variable and return that variable later on, or simply return the parent call itself.
Now, let's add some code to be ran after the function's original code.
package overwritePackage
{
function capFirstLetter(%string)
{
%parentVariable = parent::capFirstLetter(%string);
echo("This is happening after the original functionality.");
return %parentVariable;
}
};
activatePackage(overwritePackage);
Notice how we have our echo after the parent::capFirstLetter(%string); call. Code after a parent, gets executed after the original function's code. Also, notice how we stored the parent::capFirstLetter(%string); to %parentVariable. We did this because, when using return, anything below will never be executed. So we return the %parentVariable at the end of our overwritten function instead.
package overwritePackage
{
function capFirstLetter(%string)
{
%parentVariable = parent::capFirstLetter(%string);
echo("This is happening after the original functionality.");
return %parentVariable;
}
};
activatePackage(overwritePackage);
Notice how we have our echo after the parent::capFirstLetter(%string); call. Code after a parent, gets executed after the original function's code. Also, notice how we stored the parent::capFirstLetter(%string); to %parentVariable. We did this because, when using return, anything below will never be executed. So we return the %parentVariable at the end of our overwritten function instead.