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
 
I don't really understand the question. validate is a field on testClass, whose type (which isn't specified) exposes a method called checkField. Can't really say anything more than that.
 
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 :)
 
Are you just referring to having an instance of the Validate class as a field on the TestClass class? If so, you could call it composition, but Googling it won't turn up much, as it's a very broad term and is one of the most basic concepts in OOP.

Having a Validate object as a field in your class just means that a TestClass logically contains or owns a Validate object and will use it to do things (e.g. calling checkField).
 
Last edited:
It's called Method Chaining, and usually hints that there is poor design (if you are using it to access field object methods - if you are using it as in the example below, i.e. each link in the chain returns "$this" then it's ok)

http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

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?
 
Last edited:
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
}
 
Is Validate just a class that contains methods, because even with a substantiated object of class Validate its very poor OOP practice to be passing variables and not associating data w/methods.
 
Last edited:
why are you method changing?
Wouldn't this be better?

PHP:
// this is stored in the same file as firstClass.
function __autoload($class){

   require_once(dirname(__FILE__)."class.".$classname.".php");
   $something = new $class;

}

class firstClass{

 private static $instance;
 private function __construct(){}

        public function getInstance(){
                  if(!isset(self::$instance)){
                    $obj = __CLASS__;
                    self::$instance = new $obj;
                  }
        return self::$instance;
        }
 }
 

}

// Other file in same folder perhaps or w/e.
class secondClass{
  public function someFunction(){
    echo "hi";
 }
}


// call it like so in the main file.
$singleton = firstClass::getInstance();
$singleton::someFunction(); // calling the secondary class function.

i've done this very briefly so forgive me if it doesn't work, but I have got this method working in my earlier scripts.
 
They're also somewhat an antipattern in software design. Usually, they can either be replaced with static methods, or there's no need for them to be singletons at all. Remember that global state should be avoided where possible.
 
Back
Top Bottom