SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2003
    Location
    UK
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Invoking parent __call from child object

    Hi,

    I have a magic __call method in two classes and one class is the child of the other.

    The accessor methods in each class, get and set elements in their respective class arrays.

    Here's the __call code and the explanation follows it:

    Code:
    // This is the common _call method that I'm using in both the parent and child classes:
    
    function __call($strMethod, $aArgs) {
    // Dynamic get/set routine
    // $strMethod is in the format $strPropertyName
    // The property can either exist in it's own right, in the format $strPropertyName
    // or be an element of an assoc array, where the key is in the format "property_name"
    
    	$strPrefix   = substr($strMethod, 0, 3);
    	$strProperty = substr($strMethod, 3);
    	$varRet      = "";
    
    	if  (funNotNull($strPrefix)
    	and  funNotNull($strProperty)
    		):
    		if  (array_key_exists($strProperty, $this->aNonArrayVars)):
    			// This part is for properties in the format: $strPropertyName
    			$strName = $this->aNonArrayVars[$strProperty] . $strProperty;
    
    			switch ($strPrefix):
    			case "get":
    				if  (isset($this->$strName)):
    					$varRet = $this->$strName;
    				endif;
    				break;
    
    			case "set":
    				if  (isset($this->$strName)):
    					$this->$strName = $aArgs[0];
    				endif;
    				break;
    
    			endswitch;
    		else:
    			// This part is where the property is the array, in the format: "property_name",
    			// so the $strMethod value has to first be converted into this format
    			$strProp = substr($strProperty, 0, 1);
    
    			for ($i = 1; $i < strlen($strProperty); $i++):
    				$strChar = substr($strProperty, $i, 1);
    
    				if  (ctype_upper($strChar)):
    					$strProp .= "_$strChar";
    				else:
    					$strProp .= $strChar;
    				endif;
    			endfor;
    
    			$strProp = strtolower($strProp);
    
    			switch ($strPrefix):
    			case "get":
    				if  (isset($this->aArea[$strProp])):
    					$varRet = $this->aArea[$strProp];
    				endif;
    				break;
    
    			case "set":
    				if  (isset($this->aArea[$strProp])):
    					$this->aArea[$strProp] = $aArgs[0];
    				endif;
    				break;
    
    			endswitch;
    		endif;
    	endif;
    
    	return $varRet;
    }
    What I would like to do is the following:
    Code:
    			switch ($strPrefix):
    			case "get":
    				if  (isset($this->aArea[$strProp])):
    					$varRet = $this->aArea[$strProp];
    				else:
    					$varRet = parent::$strMethod($aArgs[0]);
    				endif;
    				break;
    
    			case "set":
    				if  (isset($this->aArea[$strProp])):
    					$this->aArea[$strProp] = $aArgs[0];
    				else:
    					parent::$strMethod($aArgs[0]);
    				endif;
    				break;
    
    			endswitch;
    i.e. if the specified property is not in the child's array, then I would like to pass it to the parent to satisfy the request and I would like to do it without having to physically define a get and set method for every one of the elements in the parent array, because there are an awful lot of them.

    However, what I've found is that when I try to call the parent's non-existent method from the child's __call method, the child's __call method is invoked again, instead of the parent's one. What I mean is that, if the parent method does physically exist, then this will work, but if php has to call a __call method to satisfy the request, then the child's one is used in preference to the parent's one.

    What I would like to know is how I can force the parent's __call method to be invoked, instead of the child's one.

    So, my questions are:

    1. Is it possible to have a __call method in both the child and parent classes and be able to request an element that only exists in the parent's array via the child class e.g. $child->getParentProperty()?

    2. How can you force the parent's __call method to be invoked (instead of the child's __call method being re-invoked) for a property in the parent class that doesn't have an explicit accessor method defined?

    Debbie
    QuicknEasySalesPro.com
    - your quick and easy, yet powerful solution for managing your
    membership site sales, downloads and affiliates.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see where are your parent class,I see only your FUNCTION!
    when you want to use parent class ,you must use new,then can use this class function by ->;
    e.g:
    $simpdb= new $db;
    $username=$simdb->getusername();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •