我们在生产服务器上收到PHP通知:
[PHP Notice] Trying to get property of non-object
在我们的代码中,我们有两个Foo
个对象数组,每个对象都有一个pos
成员变量:
$locked_foos
$unlocked_foos
我正在运行一个while循环来命令并将这些Foo
合并到一个新的数组结果中:
$cur_pos = 1;
$result = [];
// Extract first locked and unlocked foos
$locked_foo = reset($locked_foos);
$unlocked_foo = reset($unlocked_foos);
// While there is either an unlocked or locked foo..
while($locked_foo || $unlocked_foo){
// If there is no unlocked (thus must be a locked) or the locked's position is current..
if(!$unlocked_foo || $locked_foo->pos == $cur_pos){ // **Error**
// Use locked, set pos and iterate locked
$locked_foo->pos = $cur_pos;
$result[] = $locked_foo;
$locked_foo = next($locked_foos);
}
// Else..
else {
// Use unlocked, set pos and iterate unlocked
$unlocked_foo->pos = $cur_pos;
$result[] = $unlocked_foo;
$unlocked_foo = next($unlocked_foos);
}
// Iterate current position
$cur_pos++;
}
错误发生在显示的行上。
我认为当$locked_foos
中的一个设置为对象以外的其他内容时会发生错误,所以我添加了这些行:
if($locked_foo && !is_object($locked_foo)){
throw new Exception('FOOOO!');
}
就在while循环中。
我仍然得到相同的错误并且没有抛出异常,这怎么可能?
答案 0 :(得分:1)
原来我的逻辑很时髦:
// If there is no unlocked (thus must be a locked) or the locked's position is current..
if(!$unlocked_foo || $locked_foo->pos == $cur_pos){ // **Error**
// Use locked, set pos and iterate locked
$locked_foo->pos = $cur_pos;
$result[] = $locked_foo;
$locked_foo = next($locked_foos);
}
在$locked_foos
$unlocked_foos
之前$unlocked_foo
用尽$locked_foo = false
的情况下,if
为正// If there is a locked with the current position or there is no unlocked..
if(
$locked_foo &&
($locked_foo->pos == $cur_pos || !$unlocked_foo)
){
..
}
。
我将Condition-1: If experience is more than 10 years, increase salary by 20%.
Condition-2: If experience is greater than 5 years, increase salary by 10%.
Condition-3: All others will get an increase of 5% in the salary.
条件更改为
DECLARE
veno emp.empno%type:=&veno;
vsal emp.sal%type;
vexp number;
BEGIN
select empno,sal,trunc(to_char(months_between(sysdate,hiredate)/12))into veno,vsal,vexp from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('before update:' ||chr(10)||veno||chr(10)||vsal);
if vexp>=10 then
update emp set sal=sal+(sal*.20) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
elsif vexp>=5 then
update emp set sal=sal+(sal*.10) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
else
update emp set sal=sal+(sal*.05) where empno=veno;
select sal into vsal from emp where empno=veno;
DBMS_OUTPUT.PUT_LINE('after update:' ||chr(10)||vsal);
end if;
END;
/
这是正确的逻辑(也可能更具可读性)。