Jump to content

class/mvc


Destramic

Recommended Posts

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

Link to comment
Share on other sites

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');
     }

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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).

 

Link to comment
Share on other sites

^^ 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.

Link to comment
Share on other sites

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";
}
}

 

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.