SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ReflectionClass try/catch ............

    Hi.
    I'm wondering if I'm making
    the right think
    To check the controller

    Code PHP:
    public function map(){
    		$routeParts= $this->request->parseQueryString();
    		$controller= isset($routeParts[0])?$routeParts[0]:'defaultController';
    		$action= isset($routeParts[1])?$routeParts[1]:'defaultAction';
    		$parameters= array_slice($routeParts, 2); 
    		try{
    			$reflectionClass = new ReflectionClass($controller);
    			$parent= $reflectionClass->getParentClass();
    			if(!$reflectionClass->isSubclassOf(new ReflectionClass($parent->name))){
    				$controller= 'defaultController';	
    			}
     
    		}
    		catch(ReflectionException $e){
    			//exit($e->getMessage());
    			$controller= 'defaultController';
    		}
    		new $controller();
    	}

    Do you think is a good idea to
    use a try/catch block ?

    Do you have any advice about it ?

    By the way I'm just using __autoload()

    Bye.
    Last edited by whisher; Dec 16, 2008 at 13:14.

  2. #2
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $parent= $reflectionClass->getParentClass(); 
                if(!$reflectionClass->isSubclassOf(new ReflectionClass($parent->name))){ 
                    $controller= 'defaultController';    
                }

    Doesn't make sense to me.
    getParentClass() returns a ReflectionClass object so the new ReflectionClass($parent->name) is useless.
    Also, I don't see a property "name" with a ReflectionClass object according to the docs, use getName() instead.
    And last but not least - a class of which you received the parent class through a reflection should always be a subclass of it's parent class - so the check is useless all together.
    Or am I missing something? (Possibly, it's late and I'm tired...)

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't make sense to me.
    getParentClass() returns a ReflectionClass object so the new ReflectionClass($parent->name) is useless.
    Also, I don't see a property "name" with a ReflectionClass object according to the docs, use getName() instead.
    And last but not least - a class of which you received the parent class through a reflection should always be a subclass of it's parent class - so the check is useless all together.
    Or am I missing something? (Possibly, it's late and I'm tired...)
    Thanks for the point.
    I'm in this situation and
    I don't find other way to check
    the incoming controller
    (apart the usual allowed controller)

    PHP Code:
    abstract class  w_Controller {
        abstract public function 
    run();     
    }
    class 
    defaultController extends w_Controller{
        public function 
    __construct(){}
        public function 
    run(){
            echo 
    "defaultController";
        }
    }
    class 
    category extends w_Controller{
        public function 
    __construct(){}
        public function 
    run(){
            echo 
    "category";
        }
    }
    $controller'category';
    $reflectionClass = new ReflectionClass($controller);
    $parent$reflectionClass->getParentClass();
    var_dump($reflectionClass->isSubclassOf(new ReflectionClass($parent->getName()))); 

    What do you think about the try/catch block
    I used it instead of class_exist ?

  4. #4
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    $parent= $reflectionClass->getParentClass();
    var_dump($reflectionClass->isSubclassOf(new ReflectionClass($parent->getName())));
    [/php]
    This is still useless.
    Let's see in detail, why:

    $parent= $reflectionClass->getParentClass();
    -> $parent has a ReflectionClass object of the class w_Controller

    new ReflectionClass($parent->getName())
    -> getName() on an already instantiated ReflectionClass object to create the same type of ReflectionClass object with exactly the same class.

    $reflectionClass->isSubclassOf(new ReflectionClass($parent->getName()))
    -> Get the parent class reflection, create a reflection of the parent class, check if a subclass of $parent is a subclass of $parent

    The check all over doesn't make sense to me.
    See how Zend Framework does it.
    Or try something like this:
    $reflection = new ReflectionClass($controller);
    if($reflection->getParentClass() !== 'w_Controller') {
    $controller = 'defaultController';
    }

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by G.Schuster View Post
    This is still useless.
    Let's see in detail, why:

    $parent= $reflectionClass->getParentClass();
    -> $parent has a ReflectionClass object of the class w_Controller

    new ReflectionClass($parent->getName())
    -> getName() on an already instantiated ReflectionClass object to create the same type of ReflectionClass object with exactly the same class.

    $reflectionClass->isSubclassOf(new ReflectionClass($parent->getName()))
    -> Get the parent class reflection, create a reflection of the parent class, check if a subclass of $parent is a subclass of $parent

    The check all over doesn't make sense to me.
    See how Zend Framework does it.
    Or try something like this:
    $reflection = new ReflectionClass($controller);
    if($reflection->getParentClass() !== 'w_Controller') {
    $controller = 'defaultController';
    }
    Thanks again for the point

    I thought it was a safe code.
    I've peeped into this execelent book
    (up to now )


    http://books.google.com/books?id=qVL...&hl=it#PPP1,M1

    Take a look at pag 243.



    Bye.

    Ps.
    Your solution was the first
    think I've thaught but then
    peeping in the book ....


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
  •