Viewed   81 times

I've just came across this on GitHub.

 ($config === NULL) and $config = Kohana::config('email');

Is that the equivalent of

if ($config === NULL) {
    $config = Kohana::config('email');
}

Is this commonplace? Would I expect other developers looking at my code if I used that first way to instantly know what it was doing?

 Answers

2

Took me a second to get it, but that should actually work in just about every programming language. Because the "and" or "or" operators are lazily evaluated, if the statement on the left is false, then there's no need to evaluate the rest of the statements because the whole expression will always be false (false and true is false). Likewise, you can do it with "or", but the statement on the left would have to be true, then the one on the right wouldn't be evaluated.

PS: It doesn't really matter in this case that what's on the right isn't really a boolean expression; it'll just take on the truth value of $config

Saturday, August 27, 2022
 
4

Here are some of the shorthand operators used in PHP.

//If $y > 10, $x will say 'foo', else it'll say 'bar'
$x = ($y > 10) ? 'foo' : 'bar';

//Short way of saying <? print $foo;?>, useful in HTML templates
<?=$foo?>

//Shorthand way of doing the for loop, useful in html templates
for ($x=1; $x < 100; $x++):
   //Do something
end for;

//Shorthand way of the foreach loop
foreach ($array as $key=>$value):
   //Do something;
endforeach;

//Another way of If/else:
if ($x > 10):
    doX();
    doY();
    doZ();
else:
    doA();
    doB();
endif;

//You can also do an if statement without any brackets or colons if you only need to
//execute one statement after your if:

if ($x = 100)
   doX();
$x = 1000;

// PHP 5.4 introduced an array shorthand

$a = [1, 2, 3, 4];
$b = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
Monday, August 15, 2022
3

If you have the relationhsip in both models defined as follows:

// In artist model.
protected $_has_many = array(
        'media' => array('through' => 'media_artists'),
    );
// In media model.
protected $_has_many = array(
                'artists' => array('through' => 'media_artists'),
        );

It should work using (EDIT!):

$artists = ORM::factory('artist')->find_all();
foreach ( $artists as $artist )
{
    foreach ( $artist->media->find_all() as $m )
    {
        echo $m->name;
    }
}

I had the same problem in one of my projects and this solution works for me (Kohana 3.2.0).

Saturday, November 5, 2022
2

I think what easiest way will be write a minion task ( https://github.com/kohana/minion )

Put your task in classes/Task/ClearCache.php

<?php
class Task_ClearCache extends Minion_Task {

    protected function _execute(array $params)
    {
        Cache::instance()->delete_all();
    }
} 
?>

And run it into your BASEPATH directory > php minion.php ClearCache

Tuesday, September 27, 2022
 
ggurov
 
2

You really shouldn't try to check for non-correct PHP files at execution time : it'll kill the response time of your application !

A "better way" would be to use php -l from command line when you're done modifying a PHP script ; or include it in your build process if you're using one ; or plug it as an SVN pre-commit hook if you're using SVN and can define SVN hooks.

In my opinion, almost any given solution would be better than checking that yourself at execution time !


Considering errors like the ones you want to avoid will probably won't happen often, it is probably better to... just let them happen.
ONly thing is : activate logs, and monitor them, the be able to detect quickly when tere is a problem :-)


Of course, this doesn't prevent you from dealing with the case of missing files ; but that's a different matter...

Monday, August 22, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :