1 | <?php |
||
8 | class ProductController extends AbstractActionController |
||
9 | { |
||
10 | protected $services = array( |
||
11 | 'product' => 'speckcatalog_product_service', |
||
12 | 'product_uom' => 'speckcatalog_product_uom_service', |
||
13 | 'builder' => 'speckcatalog_builder_product_service', |
||
14 | 'configure_buy' => 'speckcatalog_configure_buy_service', |
||
15 | 'cart' => 'catalog_cart_service', |
||
16 | 'renderer' => 'zendviewrendererphprenderer', |
||
17 | 'option' => 'speckcatalog_option_service', |
||
18 | 'product_image' => 'speckcatalog_product_image_service', |
||
19 | ); |
||
20 | |||
21 | public function getService($name) |
||
22 | { |
||
23 | if (!array_key_exists($name, $this->services)) { |
||
24 | throw new \Exception('invalid service name'); |
||
25 | } |
||
26 | if (is_string($this->services[$name])) { |
||
27 | $this->services[$name] = $this->getServiceLocator()->get($this->services[$name]); |
||
28 | } |
||
29 | return $this->services[$name]; |
||
30 | } |
||
31 | |||
32 | public function indexAction() |
||
33 | { |
||
34 | $cartItemId = $this->params('cartItemId'); |
||
35 | $cartService = $this->getService('cart'); |
||
36 | $product = $this->getService('product') |
||
37 | ->setEnabledOnly(true) |
||
38 | ->getFullProduct($this->params('id')); |
||
39 | if (!$product) { |
||
40 | throw new \Exception('no product for that id'); |
||
41 | } |
||
42 | |||
43 | $this->layout()->crumbs = $this->getService('product')->getCrumbs($product); |
||
44 | |||
45 | $vars = array( |
||
46 | 'product' => $product, |
||
47 | 'editingCart' => ($cartItemId ? true : false), |
||
48 | 'cartItem' => ($cartItemId ? $cartService->findItemById($cartItemId) : false), |
||
49 | ); |
||
50 | |||
51 | return new ViewModel($vars); |
||
52 | } |
||
53 | |||
54 | public function imagesAction() |
||
55 | { |
||
56 | $productId = $this->params('id'); |
||
57 | $images = $this->getService('product_image')->getImages('product', $productId); |
||
58 | |||
59 | return new ViewModel(array('images' => $images)); |
||
60 | } |
||
61 | |||
62 | public function getViewHelper($helperName) |
||
66 | |||
67 | public function uomsPartialAction() |
||
68 | { |
||
69 | $post = $this->params()->fromPost(); |
||
78 | |||
79 | public function optionsPartialAction() |
||
94 | } |
||
95 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: