How is this possible? I am returning false, but it 'unset'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

hehehehe, same back at ya bro (= i think we both have a good understanding of how these things work. However, I think we both have an understanding about it in a different approach. I'm looking at it from a machine level, where what you are doing has already been done, so why waste a processor cycle on just defining it as a string to be called later?

IF, however, you are refering to the example that stereofrog just gave, then your argument is justified and I 100% concur.

However, doing this:

Code: Select all

function foo($bar) {
}

$foobar = 'foo';
is just an absolutely waste of time.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

volka wrote:
infolock wrote:@volka : I think you are wrong, you think I am wrong, it's all good. We'll just agree to disagree (=
There's a difference: I know you're wrong ;)
e.g.
volka wrote:

Code: Select all

foreach( pendingActions() as $tfunc) {
	$results[] = bar($tfunc, 'getRecords');
}
how many functions will bar() call and how would you implement it (without the mentioned interfaces)?

Ok... you win :oops:


Edit:
maybe.

as for your answer, it depends on how many actions you have setup to clean (however big the array is for pendingActions).

Now, if you want to call those actions via the array that is probably gonna be the result from pendingActions, and then loop through each key and treat it as a function name, then you are in fact returning to stereofrog's switch statement.



I dunno... my brain is getting torn between both sides of this argument. To be continued once I do some more testing on my own (=
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

infolock wrote:as for your answer, it depends on how many actions you have setup to clean (however big the array is for pendingActions).
There are as many actions as you want. Maybe it's a DOMNodeList. Maybe the last action manipulated the pendig actions list. Maybe it's something completely different. It's not hardcoded/hardwired.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Ok, here is the verdict.

What you are saying, is true. What I am saying is true.

What I said about this method being wrong and an improper method of coding is not true.


What I have found is that instead, this is more related to the argument between "Which is better? Switch or If".

It basically boils down to the fact that your method, while it provides a short cut, does come with it's own cost. You are doing this with a string, an unknown string at that, that must be parsed, and then validated as a proper function variable. This is because when the string initially comes in, php does not know it's a function until you specifically tell it to execute. However, this process is not tedious, nor is it bad for the engine. It's actually almost identical to a basic function call. The only difference is, again, php must access the variable that's holding hte value, parse the string, and then finally execute it.

On the other hand, you could also skip this step by doing it the method in which I'm acustomed to, which is passing an integer to represent which mode or action you want to perform, conduct a switch on it, and then finally execute the function you desire. Since the value being passed is indeed an integer, on a binary machine level this will get validated very quickly and easier than string-based values.

The only downfall with my step is the switch statement and the extra lines.

However, when it boils right down to it, both methods are basically identical in the amount of execution time.


A person could definately ruin the method I have been using by passing string values (like "Delete"), doing a switch statement on that string, and then executing based on the results. This process is much slower than an integer since string by nature take up much more processor clock cyles to compare on a binary level than integers.

So, volka, I apologize. I was wrong, but it was because the way I was attacking it was at a machine level. I could not understand just how long it would take php to acknowledge the fact that a variable's definition, when attempting to execute it, would be interpreted by php. But now that I know, I'm more knowledgable in this area, and it opens my eyes to a whole world of different possibilities. Thank you (=
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Do you know, I thought I just saw someone have an argument on the internet, change his mind, and apologise.

I must have been mistaken ;)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Very nice infolock. You don't hear too many apologies like that one around here. Well done man.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No need to apologize, no one's been hurt. ;)
And also no need for me to stop writing about what I think is still wrong :twisted:

I was writing a long text about $funcname and (oo) interfaces- similarities and differences, why you can and can't compare them- and the idea of separating code segements from one another. But this is a normal (already solved) question-thread on an internet board. So *delete long text* I will keep it short and simple.
If you reject something/an idea in general you have to either show it's completely unnecessary or provide something that can serve the same purpose.
Simple example

Code: Select all

$result[] = array_map('myFunc1', $anArray);
require 'script2.php';
$result[] = array_map(script2_function_getFunction(), $anArray);
infolock wrote:On the other hand, you could also skip this step by doing it the method in which I'm acustomed to, which is passing an integer to represent which mode or action you want to perform, conduct a switch on it, and then finally execute the function you desire.
What integer would that be for the function (name) returned by script2_function_getFunction() ? Your switch block determines what function/methods can and cannot be called, it has to know them all. Each time a new function is present the switch code has to be altered, too. Therefore the code can never be sealed and marked as safe. One semantical change, two different code places to implement the change.
That doesn't mean the switch code is useless in general and $funcname is always the better choice. It's only an example of a scenario where switch($functionNumber) has disadvantages. And it's a very realistic scenario. E.g. design patterns make heavy use of this kind of code separation (...I will probably be ostracized for comparing design patterns with $funcname... forgive me I have sinned)
infolock wrote:You are doing this with a string, an unknown string at that, that must be parsed, and then validated as a proper function variable.
Guess what php does with

Code: Select all

$a = myFunction($param);
or why does

Code: Select all

require 'script2.php';
$a = function_from_script2();
work? ;)
We're only talking about an idea and one possibility to implement that in a given language. Would I use $funcname in a larger project? Probably not. I wouldn't even encourage using it. Object interfaces provide the same functionality and more. But I use the idea. And that's not the choice between if and switch.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

volka wrote:No need to apologize, no one's been hurt. ;)
Cool (= Lots of people read a debate about an issue and automatically assume it's an argument. Just glad we're not in that boat (=

Also, just know, I respect ya. you helped me when I first started in this language 5 years ago, and I'm not blind to that fact.


With that little bit of butt kissing out of the way, let's move on.


volka wrote:What integer would that be for the function (name) returned by script2_function_getFunction() ? Your switch block determines what function/methods can and cannot be called, it has to know them all. Each time a new function is present the switch code has to be altered, too. Therefore the code can never be sealed and marked as safe. One semantical change, two different code places to implement the change.
Having you point this out made me think about my current coding methods a lot more now. I almost started to respond with "Well, this happens with switch statements all the time", and I stopped myself short. If this happens all the time with switch statements, then that means the logic in the code is flawed somewhere.

However, the reason I would still stand behind the switch method is for this reason: it's still comparing integers, which are a lot faster index searches than strings.

Even if we have to update the code, we're updating it in one spot. But a better example as to why this method would still be ok, even if we keep adding new case statements, is a simple MySQL index field. If you have an integer-based index, the search is fast and slick. If it's a varchar, it is much slower. And it's because of how many bits the processor must take to compute to gather a result.


And like you, I had to stop myself here from going on. We've said a few things over and over here. I think that we're on the same page now, we're just also still a little biased in our own personal beliefs. But that's what makes php development so unique. We could argue our sides till we're blue in the face, but I think when we came right down to it, we'd only be a few clock cycles off from each other as far as performance is concerned.

Is there anyway we could test these theories to determine just how much of a difference would be shown?

We could create a 3 line test using both methods, but I have a feeling we need to go deeper. I am sincerely interesting in seeing the outcome of this. I think it would be a realy nice eye opener for the community in general as well.


edit: in other words, I mean I don't think I've ever seen anyone conduct a true, see it to believe it, test on this subject that we are speaking of. I'd be interested in seeing it myself just to see it. know what i mean ? (=
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It still not about better or worse or faster or slower or left or right. It's something different. A different approach to solve a different problem.
I'm obviously unable to explain it and won't try any more. Sorry, but I didn't want this to become such a long thread about this (off-)topic.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

infolock wrote:We could create a 3 line test using both methods, but I have a feeling we need to go deeper.
Much deeper. But I won't.
You said that there's could never be a reason for something like $funcname. I say there is (even if $funcname is a crude approach). Let's just leave it at that.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i'm sorry, i think you got the wrong impression from my previous post...

I actually stated that I feel that you are correct, and I feel that the method is useful. And I never said there wasn't a place for $functionname.

I actualy said that I would try to use it.

In fact, I'd dropped that discussion completely, and was only wanting to see just how much of a difference there was between the two. Kind of like an experiment that we could hold as a case study. Something deeper than just dicussion.

But I'll stop as well. I think it has begun to get taken way out of line at this point.


Good dicussion though.
Post Reply