Yii Framework Forum: Troubles with deployment the project - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Troubles with deployment the project Rate Topic: -----

#1 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 24 July 2010 - 06:53 AM

Hi!

Today I decide to upload my project in hosting for test some possible bugs. But what I forgot to do is to compare the php vesions in local server and hosting. It's 5.2 in hosting and 5.3 in local. So the majority of my classes doesn't works at all.

The reason is that I use such style in my methods:
class Media extends CComponent
{

	public function Listing($mo, $crit = array(), $paging = 0)
	{

        $criterion = new CDbCriteria;
        $criterion->order = 'id DESC';

        // Additional criterion
        foreach ( $crit as $k => $v ) {
            $criterion->$k = $v;
        }

        // Enable pagination
        if ( $paging > 0 ) {

    		$qnt = $mo::model()->count($criterion);

    		$pages = new CPagination($qnt);
    		$pages->pageSize = $paging;
    		$pages->applyLimit($criterion);

        }

        $model = $mo::model()->findAll($criterion);

	return array(
            'model' => $model,
            'pages' => $pages,
        );

	}
	
	...
}


It's slice of one class. The method Listing is for helping to display the data. I pass the model name in variable $mo. So the construction $mo::model() doesn't works in 5.2 ( exception t_paamayim_nekudotayim ).

Should I repeat the same slice of code for each of five models, and only difference is the class name? I just think nobody except me does not write the code with such style :unsure:

Do you maybe know the way I can replace this stuff with more readable and flexible code?

I know it may has no relation to yii, but I don't know what if yii has some resolution in this case.
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 24 July 2010 - 03:06 PM

You can use call_user_func().
0

#3 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 24 July 2010 - 08:33 PM

Thanks Mike. I just found one more workaround ( not sure if it is workaround at all ):

$mdl = new $mo();
$qnt = $mdl->model()->count($criterion);


instead of:
$qnt = $mo::model()->count($criterion);


And it works fine in 5.2. I don't know how it may affect on performance, but I hope it doesn't at all.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 July 2010 - 03:29 AM

In essence this is even more problematic:

1. you create an object where you don't have to
2. you call a static method (class context) like a usual method (object context)

1. means, you are wasting resources, 2. means, you depend even more on PHP to allow this incorrect call of a static method (could be changed in later versions).
0

#5 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 25 July 2010 - 06:11 AM

Okay. Could you please show an example. Because I dont really understood how replace my code with call_user_func :(
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 July 2010 - 06:23 AM

Uhm, you did read the docs, did you? :)

mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )


Quote

function
The function to be called. Class methods may also be invoked statically using this function by passing array($classname, $methodname) to this parameter. Additionally class methods of an object instance may be called by passing array($objectinstance, $methodname) to this parameter.


So how about:

call_user_func(array($mo,'model'))->findAll($criterion);
// Or if you want to reuse the return value of model():
$mod=call_user_func(array($mo,'model'));
$qnt=$mod->count($criterion)
$model=$mod->findAll($criterion)

This post has been edited by Mike: 25 July 2010 - 06:25 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users