Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have developed a new technique for my future programs to decide which function is to be fired without if/else statements. It is to create an associative array of functions and call them by a function according to argument given. I think actual representation will be better.
$_functions = array(
"names" => function() {
....
},
"orders" => function() {
....
}
);
function doLoad($toLoad) {
global $_functions;
$_functions[$toLoad]();
}
Now, a user has to write only:
doLoad("names");
wherever they want to print out names.
Question:
I will be creating a script and it will be distributed among other fellas to use it their way.
Is it better to do it like this way for my script?
Is there any drawbacks of this technique?
I don't really understand what you are looking for, but I would have done it this way, for more readability. It may also be more efficient :
function doLoadNames() {
}
function doLoadOther() {
}
function doLoad($toLoad) {
$functionName = 'doLoad'.ucfirst($toLoad);
if (function_exists($functionName)) {
$functionName();
return true;
}
return false;
}
all you need is a function called call_user_func or call_user_func_array, manuals are here call_user_func, call_user_func_array
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
I have a single form.blade.php that I use to create and modify data. In that form, sometimes I have to display additional fields only in the editing state, while in the creation state they should be hidden.
I tried to define variable $mode with values edit and create in each method and it works ok. From a developer's point of view this seems like a bad solution and I wanted to consult with you on how I could do it in a better way
What i do in controller
public function edit($id)
{
$tag = Tag::find($id);
$mode = 'edit';
return view('tag.edit', compact('tag', 'mode'));
}
public function create()
{
$tag = new Tag();
$mode = 'create';
return view('tag.create', compact('tag', 'mode'));
}
And than in form.blade
#if($mode == 'edit')
....
#else
....
#endif
Do you think there is a better way to do this?
Since the create and edit mode are separate, you can send the mode right when you include the form blade
tag.edit.blade.php
#include('tag.form', ['mode' => 'edit'])
tag.create.blade.php
#include('tag.form', ['mode' => 'create'])
So you dont have to specify it in the controller.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to pass a variable from one function to another and displaying via a shortcode on the front end. This is my code
function user_last_logout(){
global $userr;
$userr = 'hi';
}
function get_logout_time(){
global $userr;
echo $userr;
}
add_shortcode('lastlogn','get_logout_time');
You can make the return of the function be the variable that you want, and then just call it inside the other function. Like so:
function user_last_logout()
{
return 'hi';
}
function get_logout_time()
{
echo user_last_logout();
}
add_shortcode('lastlogn','get_logout_time');
Is this what you are looking for?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is my method signature:
protected function updateFeeComponent(\FeeComponent $FeeList, $shipment_id, $item_id)
{
foreach($FeeList as $fee)
{
//other stuff
}
}
and I am calling it as:
$this->updateFeeComponent($shipmentFeeList,$event_id,$item_id);
where $shipmentFeeList is of #var List<FeeComponent> ItemFeeList.
How can I pass LIST of Custom object as a parameter to a function?
EDIT:
I am using PHPStorm V-9, and was integrating Laravel-Amazon-mws library
To enable type-hint / Intellicode / Available method list ( whatever you name it), I needed to mention the variable type. I fell in to this problem. I know List is not something available in PHP, but we often say List instead of Array because the logic is somewhat the same, store easily accessible multiple items (though implementation and techs differs).
So, my question was about type hinting, at the moment of asking this question, I admit, I failed to think of array instead of List...
I am neither a native English, nor an expert like most of the guys here..so, I invite everyone to edit this question in a way someone else can get his solution from this question
As far as I am aware, the closest you can get to what you want is something like:
/**
* #param FeeComponent[] $FeeList
*/
protected function updateFeeComponent(array $FeeList) {
// ...
}
Where you annotate with PHPDoc the type for $FeeList as being an array of FeeComponent and use PHP type hinting to specify the required type as being an array.
In an IDE that supports reading PHPDoc like PHPStorm, this will treat each element in the input array correctly as a FeeComponent and provide valid hinting e.g.
foreach ($FeeList as $fee) {
$fee-> // Be provided with properties and methods of FeeComponent
// in hints here.
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got the idea down, and to be so simple, I think it may be impractical, and so I would comment on the pros (if any) and cons of such a system.
The intention was to develop a simple but functional system permissions.
The problem I could see is that if you want to protect a particular function, you have to suddenly rename it to have the conf_ prefix.
I would write a proxy around a class that inspects the doc blocks when a method is invoked. Just an idea:
class ArticlesProxy
{
private $backend;
public function __construct(articles $backend)
{
$this->backend = $backend;
}
public function __call($fn, $args)
{
$rm = new ReflectionMethod($this->backend, $fn);
if (strpos($rm->getDocComment(), 'protected') !== false) {
// this method is protected by whatever
}
// perform the proxy call
return $rm->invokeArgs($args);
}
}
$proxy = new ArticlesProxy(new articles());
$proxy->create(1, 2, 3);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am using a user id , friend id , page and passing the values to a class so that it could detect that the login user can view the profile of other user based on other user's privacy setting. its such as after the values are passed to the class the query stuff takes place and i am using echo to tell if allow or dont means the output of class comes in these two words based on privacy setting . now the problem is how do i use this output to be used in a variable ?
i am passing the values into class like this
$error_result->check_allowed($friend_id,$id,$page);
now i know i get a output from that class as allow or dont . now how do i use that output further ?
In the function check_allowed(), after you do the queries, you need to return a TRUE/FALSE value using return statement. This could be used at the caller's side.
Example:
// ---- function definition inside the class
function check_allowed($friend_id,$id,$page)
{
// do some queries or whatever operation..
if($query_succeeded)
return true;
else
return false;
}
//----------------- outside the class, we are calling this function..
$result = $error_result->check_allowed($friend_id,$id,$page);
if($result == true)
{
echo "Allowed";
}
else
{
echo "Not allowed";
}
Hope this helps. Good luck :)