Destramic Posted January 12, 2011 Share Posted January 12, 2011 hey guys im wondering the best way to call a class without repeating code for instance i load my classes using the autoload function but when i want to use my FORM class i have to set it as a global to before being able to call a function is there a way without doing this all the time function __autoload($class_name) { include $class_name . '.php'; } controller class controller extends mysql { public function __contruct() { global $form; $model = new model(); // loads automatically $form = new forms(); } } class model { function test() { global $form; // repeating code again to call forms $form->validate_field('test'); } } as you can see in the model i have to set the form global as in controller before calling a function....is there a better way that what im doing please? if you dont understand please let me know thank you Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/ Share on other sites More sharing options...
Anti-Moronic Posted January 12, 2011 Share Posted January 12, 2011 it depends what pattern you are using. If you have autoloading you can simply use new forms(); anywhere you like. You don't have to declare the $form as global. You return the form, or return the model. I don't have lots and lots of experience building these types of designs from scratch so I can't advise too much, but I can say having to use global in a method means you're structure is wrong. function test() { global $form; // repeating code again to call forms $form->validate_field('test'); } ..should be: function test() { $form = new forms(); $form->validate_field('test'); } Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1158466 Share on other sites More sharing options...
Destramic Posted January 12, 2011 Author Share Posted January 12, 2011 but the form class is already called from the controller $form = new form; but then the model is added but i want to be able to call the form inside the model like $form->validate(); without having to load the class again or set a global $form = new forms(); //or global $form; Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1158491 Share on other sites More sharing options...
ignace Posted January 12, 2011 Share Posted January 12, 2011 function __autoload($class_name) { include $class_name . '.php'; } You should take a look at spl_autoload_register as this allows you to use an OO-approach to autoloading. class controller extends mysql A Controller does not extend MySQL. Such implementation details should be abstracted so that your application becomes not dependent on the presence of any specific extension/technology. class model A Model is not explicitly named Model, it's a concept. If you have ever programmed an application where you would have a file like contact.php that did the processing and called a separate file for all HTML-related stuff then you have programmed an MVC-architecture (sorta). The Controller is responsible for all Application-logic (POST, GET, ..), the Model lives in the Domain-layer and is responsible to enforce business rules (eg login after verification) and use some data store to persist information, the View has the only responsibility of showing stuff (not do any real work). Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1158506 Share on other sites More sharing options...
ignace Posted January 12, 2011 Share Posted January 12, 2011 without having to load the class again or set a global $form = new forms(); //or global $form; Meet the method argument list: $model->test($form); Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1158508 Share on other sites More sharing options...
Anti-Moronic Posted January 12, 2011 Share Posted January 12, 2011 ^^ what he said. ..on your reply, the trouble is the structure is all wrong. Read ignace's post above. You shouldn't even need the form object in the model, that belongs to the controller and will likely be passed onto the view (or a variation of) in some way (either object, array etc). Also, the way you're passing the object doesn't make sense. One instantiation should be responsible for one form. Globalizing means the $form variable could hold ANYTHING. There are many ways to effectively 'juggle' different objects. This is not one of them. Validation controls will overlap if you try to use the same object on multiple forms, it instantly compromises the integrity of the object. Validation functions should probably be accessed as a static class: formValidator::validateUsername($username); which can be called anywhere in your app as long as you have formValidator class loaded. You might consider downloading some frameworks and revising their mvc components to see some ways of approaching this. Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1158513 Share on other sites More sharing options...
Destramic Posted January 13, 2011 Author Share Posted January 13, 2011 That code about was just an example...but I think you are on the right track with what you said but how do I make the form class a static for the rest of the classes I include in the controller please? Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1158659 Share on other sites More sharing options...
Destramic Posted January 14, 2011 Author Share Posted January 14, 2011 thanks for your post i think i found what i need "STATIC" so i can just call my from methods like this form::validate_field(); unless you guys have a better idea? and also @agnace how is spl_autoload_register() any different from using function __autoload($class_name) { if (file_exists(ROOT . DS . 'library' . DS . $class_name . '.class.php')) { require_once ROOT . DS . 'library' . DS . $class_name . '.class.php'; } else if (file_exists(ROOT . DS . 'application' . DS . 'controllers' . DS . $class_name . '.class.php')) { require_once ROOT . DS . 'application' . DS . 'controllers' . DS . $class_name . '.class.php'; } else if (file_exists(ROOT . DS . 'application' . DS . 'models' . DS . $class_name . '.class.php')) { require_once ROOT . DS . 'application' . DS . 'models' . DS . $class_name . '.class.php'; } else { echo "Class " . $class_name . " failed to load.<br />\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1159441 Share on other sites More sharing options...
ignace Posted January 14, 2011 Share Posted January 14, 2011 In your specific instance you could have: spl_autoload_register(array('LibraryAutoloader', 'autoload')); spl_autoload_register(array('ApplicationAutoloader', 'autoload')); So that instead of having to add yet another if to your __autoload each new module can have it's own autoloader without having to modify any code. You should avoid static methods as much as possible as this is procedural programming and not true OO. formValidator::validateUsername($username); This actually hurts re-usability. Better would be to use a setup like Zend where there is a class for each item so that Form's and Model's can use validation/filtering. $form->addValidate('username', new Zend_Validate_Username()); $model->addValidate('username', new Zend_Validate_Username()); Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1159469 Share on other sites More sharing options...
Destramic Posted January 15, 2011 Author Share Posted January 15, 2011 do you have a better example of that class for each item please? thank you Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1159673 Share on other sites More sharing options...
ignace Posted January 15, 2011 Share Posted January 15, 2011 class Form { public function addElement(FormElement $element) { array_push($this->_elements, $element); return $element; } } $form = new LoginForm(); $element = $form->addElement(new SingleLineInputElement()); $element->addValidator(new EmailAddressValidator()); // $this->validatorChain->stopOnFirstError(false)->isValid($this->getValue()); class UserRepository { private $validationRules = array('email_address' => 'EmailAddressValidator', 'username' => 'UsernameValidator'); public function createRow() { $record = new ActiveRecord($this->getAdapter(), $this->tableColumns); $record->addValidator('email_address', new EmailAddressValidator()); return $record; } } $row = $userRepo->createRow(); $row->email_address = 'foo'; $row->save(); // ERROR: INSERT FAILED 'foo' is not a valid email address Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1159759 Share on other sites More sharing options...
Destramic Posted January 15, 2011 Author Share Posted January 15, 2011 looks complicated...i think i'll have to have a play around and have a read up because its not making much sence at the moment sorry Quote Link to comment https://forums.phpfreaks.com/topic/224207-classmvc/#findComment-1159784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.