How to create a dynamically named method - dynamic

for example:
class Foo() {
const BAR_FUNC = 'bar';
//!!!the following function name needs to use self::BAR_FUNC rather than "hardcoded" text "bar" in the function name
function get_default_bar() {
return 'this is my bar';
}
}
http://au1.php.net/functions.variable-functions has an example of dynamically named function, but it doesn't work for class method:
Create and call a dynamically named function
$tmp = "foo";
$$tmp = function() {
global $tmp;
echo $tmp;
};
$$tmp();
Outputs "foo"

The example you gave is for calling a function based on a dynamic name, not creating a function based on a dynamic name.
There is an existing answer at Dynamically Create Instance Method in PHP

Related

Ho to call variables items on Joomla Component

I'm trying to create my first component in Joomla but I can't understand how it works really :D
I create a basic component with "Component Creator". (I saved a lot of time...).
Now, I have a table in my DB where I've put my data:
id = 1
name = Andrea
note = Ciao
Now, I want to call it from the page using Joomla Language.
On my models/component.php i wrote:
class Variabili extends JModelLegacy
{
function estraivariabili()
{
$db =& JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table")->loadObjectList();
return $value;
}
}
and on my default.php I wrote
$model=$this->Variabili();
//call the method
$items=$model->estraivariabili();
//print
print_r($items);
But on the page I have this error:
0 Call to undefined method Calcolo_imposteViewCalcoloonline::Variabili()
Where is a mistake?
Please be gentle with me because I'm a beginner: D
Thanks in advance
Andrea
You have committed a few wrongs. I've rewritten your functions so that you can get it. Let's have a look-
The model, it looks almost okay. Just change it like-
class Variabili extends JModelLegacy
{
// Make the function public
public function estraivariabili()
{
$db = &JFactory::getDBO();
// Put the result into a variable first, then return it.
$value = $db->setQuery("SELECT * FROM #__table")->loadObjectList();
return $value;
}
}
And now call the model functions not from default.php, rather than write your code inside view.html.php file.
Inside the display function of view.html.php file first, get the model instance by using getModel() function.
$model = $this->getModel();
Now you can get the items by using this $model class instance.
$this->items = $model->estraivariabili();
This will bring you the data from the database table. And you can use the data at default.php file.
Just try at default.php file-
print_r($this->items);

How to return the result of a Solidity view method from a javascript function?

I want to determine a view function of a smart contract result in web3js 1.x
Suppose we have a view function getName(uint code) and want to get the name of a person having his code. So we write:
contract.methods.getName(code).call(option)
.then(...)
.catch(...);
We want to define a function to return the result name and do something with it.
How can we relate the defined function and method call?
For example:
async function name(code) {
contract.methods.getName(code).call(option)
.then(...)
.catch(...);
return ???
}
for (let i = 0; i <= 10; i++) {
let x = name(i);
// Do something with x, for example:
alert(x);
}
P.S: I know that the result is accessible inside .then body, but I want to access it inside the name function scope and return it from the function.
You already using async so you can just use await
var result = await contract.methods.getName(code).call(option)

how to call the function and display the result from tpl file in prestashop

I need to display product favorite count and some other functionalites in product detail page (product.tpl page).
But i am new to prestashop.so i am unable to find the where i declared function and i dont how to call the function form tpl file.
Here i write product favorite count code
public function favcount($id_product)
{
$sql = 'SELECT count(*) FROM `ps_favorite_product` WHERE `id_product`='.(int)$id_product.;
$result = Db::getInstance()->getRow($sql);
return $result['count'];
}
in which place i can insert the above code and how to call from product.tpl file
any one help ?
The best way is to do it in the ProductController. First you need to override it. For this create (or use the existant) /override/controllers/front/ProductController.php. You can use your function in the initContent() method:
/**
* #see ProductController::initContent()
*/
public function initContent() {
parent::initContent();
// you can place your favcount method inside the class and use it
$favCount = $this->favcount($this->product->id);
// then assign the variable to the template
$this->context->smarty->assign('favcount', $favCount);
}
Then in your template you can call the variable with: {$favcount}.

Unable to register a plain function through clientScript->registerScript in yii

I'm calling a simple javascript function via onchange
echo $form->dropDownList($model, 'job_title', $jobTypes, array('onchange'=>'jobTitle(this);')); ?>
Trying to register jobTitle via clientScript->registerScript
Yii::app()->clientScript->registerScript('jobTitle', "
function jobTitle(e) {
alert('e');
}
");
Yet I'm getting an error that jobTitle is not defined...
The Reason:
As the default position is CClientScript::POS_READY, the generated js is:
jQuery(function($) {
// ... there could be some other yii scriptlets too ...
function jobTitle(e) {
alert('e');
}
});
Which means your function jobTitle is available only within the jQuery(); function scope and not from outside it, and that's why you get the undefined error.
Solution 1:
If you use the positions : CClientScript::POS_HEAD or CClientScript::POS_BEGIN or CClientScript::POS_END with your registerScript call, i.e:
Yii::app()->clientScript->registerScript('jobTitle', "
function jobTitle(e) {
alert('e');
}
", CClientScript::POS_END);
the function will be defined outside, and in the global scope, and then you will be able to use the function.
Solution 2:
Alternatively if you need the function within ready(), i.e within jQuery(function($){});, you can define the function in the global window object, and still access it from outside:
Yii::app()->clientScript->registerScript('jobTitle', "
window.jobTitle = function jobTitle(e) {
alert('e');
}
");
Solution 3:
Or you can simply add an event handler from jQuery itself, instead of doing it inline in the html:
Yii::app()->clientScript->registerScript('jobTitle', "
$('body').on('change', 'id-of-job-title-input', function(e){
console.log(e);
});
");

Returning external data from a function in ActionScript

I have the following script that is calling a text file:
/* first create a new instance of the LoadVars object */
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(getreading):String{
var ODOMETER2:String=myVariables.ACADEMICWATER;
return ODOMETER2;
trace (ODOMETER2);
}
trace(getreading());
The text file contains the following:
ACADEMICWATER=3002&elec=89
I am able to import the value of 3002 into the function and I can trace it. However, I Should be able to trace it outside the function using trace(getreading()); as shown on the last line. This only returns an "UNDEFINED" value. I am stumped.
You are declaring an anonymous function (see AS3 Syntax and language / Functions) which can't be referenced by name. getreading is declared in your code as an untyped parameter of this function.
If you want to trace the result of this function, then you should declare a named function like this:
function getReading(): String {
var ODOMETER2:String=myVariables.ACADEMICWATER;
return ODOMETER2;
}
myVariables.onLoad = getReading;
trace(getReading());
getreading is not the name of the function in this case, but the name of a parameter to the anonymous function that is run on the onLoad event of the myVariables object.
Place the variable ODOMETER2 outside the function and set it's value inside the anonymous function. Then you will be able to access it outside the function as well.
/* first create a new instance of the LoadVars object */
var ODOMETER2:String;
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(){
ODOMETER2=myVariables.ACADEMICWATER;
}
trace(ODOMETER2);
LoadVars.onLoad is an event handler. It is called by LoadVars as soon as it finishes with the asynchronous load operation. It takes a boolean argument, indicating success or failure of the operation. It does not return anything.
LoadVars.onLoad documentation
In that function, you typically act upon the data you received, like storing and processing it. Here's a very simple example showing some basic use cases:
var ODOMETER2:String;
var myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(success) {
trace(success);
ODOMETER2 = myVariables.ACADEMICWATER;
processResults();
}
function processResults() {
trace(ODOMETER2);
trace(myVariables.ACADEMICWATER);
}
// traces:
// true
// 3002
// 3002