根据PHP manual在定义之前调用用户定义的函数不会产生错误,除了:
当以条件方式定义函数时......必须在被调用之前处理它的定义。
示例:
input_shape
引发致命错误:
Traceback (most recent call last):
File "trajecgen_keras.py", line 131, in <module>
tf.app.run()
File "/home/.../lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 124, in run
_sys.exit(main(argv))
File "trajecgen_keras.py", line 85, in main
input_shape=train_data.shape))
File "/home/.../lib/python3.5/site-packages/keras/models.py", line 467, in add
layer(x)
File "/home/.../lib/python3.5/site-packages/keras/engine/topology.py", line 619, in __call__
output = self.call(inputs, **kwargs)
File "/home/.../lib/python3.5/site-packages/keras/layers/wrappers.py", line 211, in call
y = self.layer.call(inputs, **kwargs)
File "/home/.../lib/python3.5/site-packages/keras/layers/convolutional.py", line 168, in call
dilation_rate=self.dilation_rate)
File "/home/.../lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3335, in conv2d
data_format=tf_data_format)
File "/home/.../lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 753, in convolution
name=name, data_format=data_format)
File "/home/.../lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 799, in __init__
input_channels_dim = input_shape[num_spatial_dims + 1]
File "/home/../lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 521, in __getitem__
return self._dims[key]
IndexError: list index out of range
通过上面的解释,我想在下面的代码中产生相同的错误:
bar();
$makefoo = true;
if ($makefoo) {
function bar() {
echo "I don't exist.\n";
}
}
但是it doesn't。在描述此行为的手册中我缺少什么?
答案 0 :(得分:4)
foo();
bar();
function foo() {
$makefoo = true;
if ($makefoo) {
function bar() {
echo "I don't exist until foo() is called.\n";
}
}
}
PHP代码分两次执行:首先是解析和编译步骤,然后是运行时。像foo
这样的普通函数可以在解析时定义;它们是无条件的,不依赖于运行时信息。
因此function foo
是在解析时定义的,然后在运行时foo()
被调用,这会创建function bar
,因此您可以在之后立即调用bar()
。