PHP OOP Question

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

Can you someone tell me what the following is called and breifly how it is achieved, using PHP - kind of like accessing the various nodes of an XML document, expect done with classes (in the example, validate would be a called called validate with a method called checkField:

Code:
$obj = new testClass();
$obj->validate->checkField('name');

Ta
 
Sorry, will explain a bit better :)

Say I had a class:

PHP:
class TestClass() {

   function someFunction() {
     return 10*2;
  }

}
and another class:

PHP:
class Validate() {

   function checkField($field) {
     return true;
  }

}
Say I create a new function in testclass I could use methods from the validate class as follows:

PHP:
class TestClass() {

   //other functions
   function somethingNew() {
      return $this->validate->checkField('yada');
  }

}
Just wondered what this type of working is called so I can Google, as I've only ever seen this way of accessing methods in bigger PHP frameworks.

The actual functions I've created could have been anything, I've used some simple one's to illustrate :)
 
It's not method chaining as $this->validate (or $obj->validate) is a field, not a method. No methods are being chained here. Unless I'm misunderstanding you?

@suarve: Do you mean that $this->validate can be used to access the methods in Validate without needing to be explicitly initialized?

I'll post the code (PHP4 code I think) that I came across. I thought it was a really usefull little feature. Yesterday I discovered the __autoload() method and redesigned a couple of classes, so I don't actually need this. However, below is where I originally saw this effect. (see register function) E.g.

PHP:
class Login {

function __construct () {
                session_start ();
                $this->_load_class ( 'functions' );
                $this->_load_class ( 'form_validation' );
                $this->_load_class ( 'db' );
        }
         
        function _load_class ( $class, $params = NULL )
        {
                $class = str_replace ( EXT, '', $class );
                
                $is_duplicate = FALSE;
                
                $fp = BASE_PATH . 'lib/' . $class . EXT;
                
                if ( ! file_exists ( $fp ) )
                {
                        die ( $this->functions->Lang ( 'file_not_exists' ) . ' ' . $fp );
                }

                // Safety:  Was the class already loaded by a previous call?
                if ( in_array ( $class, $this->_classes ) )
                {
                        return;
                }
                else {
                        include ( $fp );
                        $this->_classes [] = $class;
                        return $this->_init_class ( $class, $params );
                }
        }
        
      


         
        function _init_class ( $class, $params = FALSE )
        {
                $class = strtolower ( $class );
                if ( $params != FALSE )
                {
                        extract ( $params );
                        $this->$class = new $class ( $params );
                }
                else {
                        $this->$class = new $class (  );
                }
        }



function register() {
 $this->form_validation->add_field ( 'First_name', 'required', $this->functions->Lang ( 'fname_req' ) );
 $someCount = $this->db->RecordCount($someSQL);
}



...other functions
}
 
Back
Top Bottom