Re: [fw-general] ZFW OO coding guideline
Yes that's exactly what I was doing as the original post had no details or actual requirements. I read his question as follows: I need to call this global function somewhere inside ZF code, how to do it in a ZF way? That functionality most likely belongs somewhere, maybe in a view helper or action helper, etc. My point was that you should follow basic OOP principles to make your code consistent with ZF. Having a functional object that does some heavy stuff upon construction and provides this go method that you call all over the place because it's available globally is not the way write maintainable code in my opinion. Such code would never be up to ZF standards which even with some flaws sets the bar pretty high. I don't think that's silly. The singletons you mentioned are implemented in such way for a reason, I'm sure the idea was to force 1 registry and 1 front controller at a time. So the context matters a lot in this case. I think it comes down to lack of understanding of OOP and fundamentals that ZF is built upon. Karol Matthew Ratzloff wrote: My point was that Karol was making a broad statement without knowing the actual details of the original poster's actual requirements. There are, of course, ways to keep static methods in Zend_Registry et al and make them testable at the same time. You just have to build the functionality into the class instead of mocking it directly. I'm not necessarily advocating for this; singletons have their place, but obviously a framework's testability requirements are higher than most application code. -Matt On Sun, Jun 21, 2009 at 5:59 PM, Matthew Weier O'Phinney matt...@zend.comwrote: -- Matthew Ratzloff m...@builtfromsource.com wrote (on Sunday, 21 June 2009, 05:22 PM -0700): How silly. What is Zend_Registry, then? Zend_Controller_Front? They're all singletons. Singletons, yes. Should ZF have them? I'm leaning towards, no, as they are hard to test, and harder still for users to test their code against. That's a story for another day, however. Should all factories be implemented as instance methods, too? No -- factories serve a very different purpose, and a static method is often a good solution. Let's say this class he's talking about returns an ad for a given set of dimensions and providers. What is so wrong with My_Ad::factory(), with a view helper My_View_Helper_Ad ($this-ad()) that proxies to it (returning a My_Ad_SomeNetwork object with render() and __toString() methods)? Nothing's wrong. THe code as written, however, was poor as it was using PHP 4 paradigms (using a method with the class name as the constructor, calling methods by reference, using static within the method instead of defining a static class property...) instead of PHP 5's OOP model. I'd have written the same class as follows: class MyClass { private $_instance; private function __construct() { $this-init(); } public static function getInstance { if (null === self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } public function init() { // Some heavy init codes... } public function foo() { // core function } } This creates a singleton using PHP 5 paradigms, and ensures appropriate visibility. Using the public init() method is a nice touch, as it allows subclassing in order to alter the initialization behavior. On Sun, Jun 21, 2009 at 4:50 PM, Karol Grecki kgre...@gmail.com wrote: global functional object sounds like a very fancy name for procedural code with global functions. Code like this will never be consistent with ZF and is generally considered bad. Deciding how to call a method should probably be the least of your concerns. You should also write PHP5 code if you want to be consistent with ZF. Karol -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/ -- View this message in context: http://www.nabble.com/ZFW-OO-coding-guideline-tp24136353p24145229.html Sent from the Zend Framework mailing list archive at Nabble.com.
[fw-general] ZFW OO coding guideline
Hello, It is not a 100% zfw question, but as I am coding under zfw, so I want if my OO coding is consistent with the zfw. Example: I have a Class - MyClass, it is a global functional object, has the function foo() I need to call in several php files for a single HTTP request. class MyClass{ function MyClass () { $this-init(); } function getInstance { static $instance; if (!isset($instance)) { $instance = new MyClass(); } return $instance; } function init() { // Some heavy init codes... } function foo() { // core function } } Which one you prefer: 1. Call as global class: $data = MyCalss::foo(); // put init() in bootstrap 2. Call as singleton $myClass = MyClass::getInstance(); $myClass-foo(); 3. Call as new object $myClass = new MyClass(); $myClass-foo(); which method you prefer? Thanks.
Re: [fw-general] ZFW OO coding guideline
If it's just a namespaced function, MyClass::foo() is probably fine. BTW, unless you're using PHP 4, the pass-by-reference stuff is unnecessary. PHP 5 always passes objects by reference. -Matt On Sun, Jun 21, 2009 at 9:50 AM, howard chen howac...@gmail.com wrote: Hello, It is not a 100% zfw question, but as I am coding under zfw, so I want if my OO coding is consistent with the zfw. Example: I have a Class - MyClass, it is a global functional object, has the function foo() I need to call in several php files for a single HTTP request. class MyClass{ function MyClass () { $this-init(); } function getInstance { static $instance; if (!isset($instance)) { $instance = new MyClass(); } return $instance; } function init() { // Some heavy init codes... } function foo() { // core function } } Which one you prefer: 1. Call as global class: $data = MyCalss::foo(); // put init() in bootstrap 2. Call as singleton $myClass = MyClass::getInstance(); $myClass-foo(); 3. Call as new object $myClass = new MyClass(); $myClass-foo(); which method you prefer? Thanks.
Re: [fw-general] ZFW OO coding guideline
global functional object sounds like a very fancy name for procedural code with global functions. Code like this will never be consistent with ZF and is generally considered bad. Deciding how to call a method should probably be the least of your concerns. You should also write PHP5 code if you want to be consistent with ZF. Karol howard chen wrote: Hello, It is not a 100% zfw question, but as I am coding under zfw, so I want if my OO coding is consistent with the zfw. Example: I have a Class - MyClass, it is a global functional object, has the function foo() I need to call in several php files for a single HTTP request. class MyClass{ function MyClass () { $this-init(); } function getInstance { static $instance; if (!isset($instance)) { $instance = new MyClass(); } return $instance; } function init() { // Some heavy init codes... } function foo() { // core function } } Which one you prefer: 1. Call as global class: $data = MyCalss::foo(); // put init() in bootstrap 2. Call as singleton $myClass = MyClass::getInstance(); $myClass-foo(); 3. Call as new object $myClass = new MyClass(); $myClass-foo(); which method you prefer? Thanks. -- View this message in context: http://www.nabble.com/ZFW-OO-coding-guideline-tp24136353p24140060.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] ZFW OO coding guideline
How silly. What is Zend_Registry, then? Zend_Controller_Front? They're all singletons. Should all factories be implemented as instance methods, too? Let's say this class he's talking about returns an ad for a given set of dimensions and providers. What is so wrong with My_Ad::factory(), with a view helper My_View_Helper_Ad ($this-ad()) that proxies to it (returning a My_Ad_SomeNetwork object with render() and __toString() methods)? -Matt On Sun, Jun 21, 2009 at 4:50 PM, Karol Grecki kgre...@gmail.com wrote: global functional object sounds like a very fancy name for procedural code with global functions. Code like this will never be consistent with ZF and is generally considered bad. Deciding how to call a method should probably be the least of your concerns. You should also write PHP5 code if you want to be consistent with ZF. Karol howard chen wrote: Hello, It is not a 100% zfw question, but as I am coding under zfw, so I want if my OO coding is consistent with the zfw. Example: I have a Class - MyClass, it is a global functional object, has the function foo() I need to call in several php files for a single HTTP request. class MyClass{ function MyClass () { $this-init(); } function getInstance { static $instance; if (!isset($instance)) { $instance = new MyClass(); } return $instance; } function init() { // Some heavy init codes... } function foo() { // core function } } Which one you prefer: 1. Call as global class: $data = MyCalss::foo(); // put init() in bootstrap 2. Call as singleton $myClass = MyClass::getInstance(); $myClass-foo(); 3. Call as new object $myClass = new MyClass(); $myClass-foo(); which method you prefer? Thanks. -- View this message in context: http://www.nabble.com/ZFW-OO-coding-guideline-tp24136353p24140060.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] ZFW OO coding guideline
-- Matthew Ratzloff m...@builtfromsource.com wrote (on Sunday, 21 June 2009, 05:22 PM -0700): How silly. What is Zend_Registry, then? Zend_Controller_Front? They're all singletons. Singletons, yes. Should ZF have them? I'm leaning towards, no, as they are hard to test, and harder still for users to test their code against. That's a story for another day, however. Should all factories be implemented as instance methods, too? No -- factories serve a very different purpose, and a static method is often a good solution. Let's say this class he's talking about returns an ad for a given set of dimensions and providers. What is so wrong with My_Ad::factory(), with a view helper My_View_Helper_Ad ($this-ad()) that proxies to it (returning a My_Ad_SomeNetwork object with render() and __toString() methods)? Nothing's wrong. THe code as written, however, was poor as it was using PHP 4 paradigms (using a method with the class name as the constructor, calling methods by reference, using static within the method instead of defining a static class property...) instead of PHP 5's OOP model. I'd have written the same class as follows: class MyClass { private $_instance; private function __construct() { $this-init(); } public static function getInstance { if (null === self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } public function init() { // Some heavy init codes... } public function foo() { // core function } } This creates a singleton using PHP 5 paradigms, and ensures appropriate visibility. Using the public init() method is a nice touch, as it allows subclassing in order to alter the initialization behavior. On Sun, Jun 21, 2009 at 4:50 PM, Karol Grecki kgre...@gmail.com wrote: global functional object sounds like a very fancy name for procedural code with global functions. Code like this will never be consistent with ZF and is generally considered bad. Deciding how to call a method should probably be the least of your concerns. You should also write PHP5 code if you want to be consistent with ZF. Karol howard chen wrote: Hello, It is not a 100% zfw question, but as I am coding under zfw, so I want if my OO coding is consistent with the zfw. Example: I have a Class - MyClass, it is a global functional object, has the function foo() I need to call in several php files for a single HTTP request. class MyClass{ function MyClass () { $this-init(); } function getInstance { static $instance; if (!isset($instance)) { $instance = new MyClass(); } return $instance; } function init() { // Some heavy init codes... } function foo() { // core function } } Which one you prefer: 1. Call as global class: $data = MyCalss::foo(); // put init() in bootstrap 2. Call as singleton $myClass = MyClass::getInstance(); $myClass-foo(); 3. Call as new object $myClass = new MyClass(); $myClass-foo(); which method you prefer? Thanks. -- View this message in context: http://www.nabble.com/ ZFW-OO-coding-guideline-tp24136353p24140060.html Sent from the Zend Framework mailing list archive at Nabble.com. -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/
Re: [fw-general] ZFW OO coding guideline
My point was that Karol was making a broad statement without knowing the actual details of the original poster's actual requirements. There are, of course, ways to keep static methods in Zend_Registry et al and make them testable at the same time. You just have to build the functionality into the class instead of mocking it directly. I'm not necessarily advocating for this; singletons have their place, but obviously a framework's testability requirements are higher than most application code. -Matt On Sun, Jun 21, 2009 at 5:59 PM, Matthew Weier O'Phinney matt...@zend.comwrote: -- Matthew Ratzloff m...@builtfromsource.com wrote (on Sunday, 21 June 2009, 05:22 PM -0700): How silly. What is Zend_Registry, then? Zend_Controller_Front? They're all singletons. Singletons, yes. Should ZF have them? I'm leaning towards, no, as they are hard to test, and harder still for users to test their code against. That's a story for another day, however. Should all factories be implemented as instance methods, too? No -- factories serve a very different purpose, and a static method is often a good solution. Let's say this class he's talking about returns an ad for a given set of dimensions and providers. What is so wrong with My_Ad::factory(), with a view helper My_View_Helper_Ad ($this-ad()) that proxies to it (returning a My_Ad_SomeNetwork object with render() and __toString() methods)? Nothing's wrong. THe code as written, however, was poor as it was using PHP 4 paradigms (using a method with the class name as the constructor, calling methods by reference, using static within the method instead of defining a static class property...) instead of PHP 5's OOP model. I'd have written the same class as follows: class MyClass { private $_instance; private function __construct() { $this-init(); } public static function getInstance { if (null === self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } public function init() { // Some heavy init codes... } public function foo() { // core function } } This creates a singleton using PHP 5 paradigms, and ensures appropriate visibility. Using the public init() method is a nice touch, as it allows subclassing in order to alter the initialization behavior. On Sun, Jun 21, 2009 at 4:50 PM, Karol Grecki kgre...@gmail.com wrote: global functional object sounds like a very fancy name for procedural code with global functions. Code like this will never be consistent with ZF and is generally considered bad. Deciding how to call a method should probably be the least of your concerns. You should also write PHP5 code if you want to be consistent with ZF. Karol howard chen wrote: Hello, It is not a 100% zfw question, but as I am coding under zfw, so I want if my OO coding is consistent with the zfw. Example: I have a Class - MyClass, it is a global functional object, has the function foo() I need to call in several php files for a single HTTP request. class MyClass{ function MyClass () { $this-init(); } function getInstance { static $instance; if (!isset($instance)) { $instance = new MyClass(); } return $instance; } function init() { // Some heavy init codes... } function foo() { // core function } } Which one you prefer: 1. Call as global class: $data = MyCalss::foo(); // put init() in bootstrap 2. Call as singleton $myClass = MyClass::getInstance(); $myClass-foo(); 3. Call as new object $myClass = new MyClass(); $myClass-foo(); which method you prefer? Thanks. -- View this message in context: http://www.nabble.com/ ZFW-OO-coding-guideline-tp24136353p24140060.html Sent from the Zend Framework mailing list archive at Nabble.com. -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/