Jump to content

using $variable = function() { RETURN 'HELLO WORLD'; }


Monkuar
Go to solution Solved by Ch0cu3r,

Recommended Posts

This is object programming right?

 

Is there a performance issue with this?

 

for example:

$notfications = ( blah blah ) ? : '';
then
if ($notificatiosn != blabla){
echo 'hey';
}
or....

$notifications = function(){
do my stuff here
then return 'hey';
}
Which way is faster or slower? Reason I Ask this is because I've been doing some object orientated programming in javascript, and didn't know you could do it in php.

 

I'd rather do the objective way so I don't have to use so many freaking variables above the regular way, lol.

Edited by Monkuar
Link to comment
Share on other sites

Also, when using this I get:

 

 Catchable fatal error: Object of class Closure could not be converted to string in C:\WT-NMP\WWW\header.php on line 364
Yeah, no idea. Never seen this error. I have a feeling I can only use the object orientated functions inside a Class right? If so, nevermind my question -.-
Link to comment
Share on other sites

  • Solution

 

This is object programming right?

Nope

 

The first block of code uses ternary operator (aka inline if/else statement) and a if statement.

Second block of code uses an anonymous function.

 

None of which features object oriented programming.  

 

 

Which way is faster or slower?

Why would that matter? You will not be able to notices the differences between the two.

 

 

Reason I Ask this is because I've been doing some object orientated programming in javascript, and didn't know you could do it in php.

 

object orientated programming in javascript is not the same as object orientated programming in PHP.

  • Like 1
Link to comment
Share on other sites

No that has nothing to do with OOP. Your first example is using the ternary comparison operator, which is just a short way if writing if/else statements (javascript has that as well). Your 2nd code block is called an anonymous function.

Yeah, but Anonymous functions exist inside objective orientated programming right?

Link to comment
Share on other sites

Yeah, but Anonymous functions exist inside objective orientated programming right?

Yes, but so do ternary operators and most other PHP commands. Just because they are used within a class (which is OOP) doens't make them "oop". Echo works in a class too, but doesn't mean it's "oop". Creating and using classes is OOP. The class itself.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.