check out my new blog at gmarik.info/blog

Monday, May 23, 2005

catch Exception from __autoload

check this workaround :



<?php

function __autoload($name){
if (@!fopen($name.".php5","r")){
eval("class $name { function __construct(){throw new Exception('$name is not found');}}");
}
else {
require_once($name.".php5");
echo "\n".$name." loaded";
}
}

try{
$n = new NonExistantClass();
}catch(Exception $e){
echo $e->getMessage();
}

try{
$a = "echo hello";
$n = new $a();
$n = new test();

}catch(Exception $e){
echo $e->getMessage();
}

?>


Do not forget to echo ' <?php class test {} ?>' > test.php5 (this will create basic class test inside test.php5.

 check out my new blog at gmarik.info/blog

Tuesday, May 03, 2005

ShortestSquareSum

Yepp, another task solved...Cool!


<?php
/*
* @Algorithm : Shortest Sum of Squares
* @Author : mrix
* @Date : 30.04.2005
* @Ver : 1.1
*
* @Remark : not the most efficient, might be improoved
*/

/*
* $N - target numer
* @returns : stack of subcalculations
*/
function searchSSS($N){
if (1 == $N) return array(1);
$n = (int)sqrt($N);
if ($N == $n*$n) return array($n);

$solution = array();

$k = $n;
$r = $n;
while ($k > (int)$n/2){

$result = searchSSS($N - $k*$k);

if (0 == count($solution) || count($solution) > count($result)){
$solution = $result;
$r = $k;
}

if (count($solution) < start =" time();">