3

In javascript I can pass an object literal to an object as a parameter and if a value does not exist I can refer to a default value by coding the following;

this.title = params.title || false;

Is there a similar way to do this with PHP?

I am new to PHP and I can't seem to find an answer and if there is not an easy solution like javascript has, it seems pure crazy to me!!

Is the best way in PHP to use a ternary operator with a function call?

isset($params['title']) ? $params['title'] : false;

Thanks

9
  • 1
    Ternary is the best way. No such functionality in PHP. It's another one of those unnatural JS things... Commented Jul 27, 2013 at 8:26
  • @Code "Unnatural"? Really? Commented Jul 27, 2013 at 8:33
  • @deceze Yes because || (aka OR) should be a bool. Like this. It is in C/C++, where real programming happens. Commented Jul 27, 2013 at 8:35
  • @Code Seriously? "Real" programming? Dude, embrace different languages. JS's || is a very interesting operator which enables very interesting code; it's not any less "real" than a traditional boolean ||. Commented Jul 27, 2013 at 8:37
  • 1
    @Code You really have a weird idea of what distinguishes "real" programming... You're an Uphill in the snow, both ways guy, eh? Commented Jul 27, 2013 at 8:43

5 Answers 5

4

Don't look for an exact equivalent, because PHP's boolean operators and array access mechanism are just too different to provide that. What you want is to provide default values for an argument:

function foo(array $params) {
    $params += array('title' => false, ...);

    echo $params['title'];
}
Sign up to request clarification or add additional context in comments.

7 Comments

No it does not, that's the point: php.net/manual/en/language.operators.array.php
I keep staring and I don't see the default argument value in your example function :) You're just enforcing the argument type and prefilling the array. That's not really how default arguments work.
@Code You seem to be very hung up on one specific way to do things. Do you have a better suggestion for establishing default array values?
@deceze haha, just to clarify if title is set, adding the existing title does nothing, if it is not set then it gets assigned? Thanks
@deceze I would use explicit arguments, not arrays. And default their values. This is the WordPress way of doing things. Stick all arguments in an array so you need to LEARN or SEEK them all before using a function. When you could break them nicely apart in as arguments or an object properties and that allows friendly code completion too. I'm hung on reusability and IDE code completion friendliness.
|
2

somethig like this $title = (isset($title) && $title !== '') ? $title : false;

Comments

2

Or using the empty function:

empty($params['title']) ? false : $params['title'];

Comments

0

$x = ($myvalue == 99) ? "x is 99": "x is not 99";

PHP one liner if ... if ($myvalue == 99) {x is 99} else {x is not 99 //set value to false here}

Comments

0
<?php
class MyObject {
    // Default value of object property
    public $_title = null;
    // Default value of argument (constructor)
    public function __construct($title = null){
        $this->_title = $title;
    }
    // Default value of argument (setter)
    public function setTitle($title = null){
        // Always validate arguments if you're serious about what you're doing
        if(!is_null($title) and !is_string($title)){
            trigger_error('$title should be a null or a string.', E_USER_WARNING);
            return false;
        }
        $this->_title = $title;
        return true;
    }
} // class MyObject;
?>

This is how you do an object with default values. 3 ways in 1. You either default the property value in the class definition. Or you default it on the __construct assignment or in a specific setter setTitle.

But it all depends on the rest of your code. You need to forget JS in order to properly use PHP. This is a slightly stricter programming environment, even if very loose-typed. We have real classes in PHP, not imaginary function classes elephants that offer no IDE code-completion support like in JS.

2 Comments

Although I agree, I think classes defer from true programming. True prototypal inheritance is alot cleaner, efficient, and more true. Prototypal inheritance enforces a has a relationship, rather classes that extend multiple classes and enforce a is a relationship, when we all know that some classes should extend another one because they do not have that has a relationship.
@GriffLab I think classes defer from true programming - this made my day. Spoken like a true C coder. You're really missing out... Classes = procedures + structures of C made whole again. Plus you have the beautiful destructor where you can clean things up and not have to track it everywhere. In C++ you also have the template classes which blow anyone's mind in terms of code reusability. I'm not an objectualize all things maniac but I know the benefits of a class/object. I did start out in C, 1.5 decades ago...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.