Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

in one class run many class, PHP OOP -


i have class "user_registration" , in class need use many class: "location", "links", "mail", "module".

i create include class in file: include 'class/location.php'; include 'class/links.php'; include 'class/mail.php'; include 'class/modules.php';

now create "user_registration" class.

<?php class user_registration{      public function insert_to_db($name, $country_code, $home_page)     {         //work data          return id;     }      public function show_info($id)     {         //work data     }  }  $reg_u = new user_registration; $res = $reg_u->insert_to_db($name, $country_code, $home_page);  if($res){     $reg_u->show_info($res); } ?> 

i need in method "insert_to_db" run class: "location", "links", "mail" methods , in "show_info" run methods of "location", "links", "module" class.

how? how in 1 class run class (no't one)

thanks ;)

there few ways this. if have couple objects class needs utilize, use dependency injection; pass each object argument class's constructor , store objects class property.

if single method needs object, pass object argument of method. discourage approach though, because feel hinders expandability/code-cleanliness in long run.

if have many objects needed in several classes, recommend registry inject class's constructor. registry singleton (it holds single instance of each object need share). in class needs utilize shared object, might call $this->registry->get('some_shared_object')->dosomething().


dependency injection (at constructor)

class foo {     protected $dependency1;     protected $dependency2;     protected $dependency3;      public function __construct($dependency1, $dependency2, $dependency3) {         $this->dependency1 = $dependency1;         $this->dependency2 = $dependency2;         $this->dependency3 = $dependency3;     }      public function foo() {         $this->dependency1->dosomething();     } }  $foo = new foo($dependency1, $dependency2, $dependency3); $foo->foo();

dependency injection (at method, not recommended)

class foo {     public function foo($dependency1) {         $dependency1->dosomething();     } }  $foo = new foo(); $foo->foo($dependency1);

dependency injection using registry

class registry {     var $data = array();      function __get($key) {         return $this->get($key);     }      function __set($key, $value) {         $this->set($key, $value);     }      /**     * retrieve resource registry.     *     * @param string     * @return mixed|null     */     function get($key) {         return isset($this->data[$key]) ? $this->data[$key] : null;     }      /**     * store resource in registry.     *     * @param string     * @param mixed     */     function set($key, &$value) {         $this->data[$key] = $value;     }      /**     * check if resource exists in registry.     *     * @param string     * @return boolean     */     function has($key) {         return isset($this->data[$key]);     } }  class foo {     protected $registry;      public function __construct($registry) {         $this->registry = $registry;     }      public function foo() {         $this->registry->dependency1->dosomething();     } }  $dependency1 = new dependency1(); $registry = new registry(); $registry->set('dependency1', $dependency1);  $foo = new foo($registry); $foo->foo();

Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -