This is a discussion on finding compile time errors within the PHP Language forums, part of the PHP Programming Forums category; Jeff wrote: > -- SNIP -- > > I'd like to just pass in an associative array to a function and ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Jeff wrote:
> -- SNIP -- > > I'd like to just pass in an associative array to a function and have > it return the form widget. > > My first php class is looking like this (feel free to add constructive > criticism): > > class FormWidgets{ > > public function __construct($A){ > if($A['type'] == 'select'){ > return makePopupMenu($A); > } > } > > public function makePopupMenu($A){ > > $values = $A['values']; > $labels = $A['labels']; > > $content = '<select name="' . $A['name'] . '">' . "\n"; > > foreach($values as $item){ > $selected_ = ''; > > if($A['value'] == $item){$selected_ = 'selected';} > $label_ = $labels[$item]; > if(! $label_){$label_ = $item;} > $content .= '<option value="' . $item . '"' . $selected_ . '>' . > label_ .. '</option>' . "\n"; > > > } > > $content .= '</select>'; > return $content; > > } > > > } // end class > > > > You'd call it like this, I think: > > $form_widget = new FormWidgets; > > $popup_menu = > $form_widget->makePopupMenu('name'=>'some_name','values'=>array ('item > 1','item 2'),'labels'=>array(...)...); Well, I kinda see how you want to do that, but IMHO you are not taking advantage of the language, as you are trying to do it PERL way... However there is one error that I can spot on your class declaration -- Constructor will always return an instance of the class no matter what you return > -- SNIP -- > > > Hmmm. I normally have a small configure file for each server which has > a database handle and paths in it. Can I just name a file with the pdo > extension and will php use it (if it's in the library path) without have > to call include or require? Should I be using pdo for my "module" > extension? Those "modules" will just have classes. No, you need to use "include" or "require"... Yes, you can use PDO to access database, as it provides abstraction layer to access database. There are some people who does not like to use it, citing performance issues. > -- SNIP -- > > Do you know if the the include_once is like the perl 'do'? Hmmm, PHP takes a more simplistic approach to it's include_once. Anything that you tell PHP under include_once directive will simply means to include the specified files to be executed too... Please refer to PHP manual for more info. Hendri Kurniawan |
|
|||
|
Hendri Kurniawan wrote:
> Jeff wrote: >> > -- SNIP -- >> >> I'd like to just pass in an associative array to a function and have >> it return the form widget. >> >> My first php class is looking like this (feel free to add constructive >> criticism): >> >> class FormWidgets{ >> >> public function __construct($A){ >> if($A['type'] == 'select'){ >> return makePopupMenu($A); >> } >> } >> >> public function makePopupMenu($A){ >> >> $values = $A['values']; >> $labels = $A['labels']; >> >> $content = '<select name="' . $A['name'] . '">' . "\n"; >> >> foreach($values as $item){ >> $selected_ = ''; >> >> if($A['value'] == $item){$selected_ = 'selected';} >> $label_ = $labels[$item]; >> if(! $label_){$label_ = $item;} >> $content .= '<option value="' . $item . '"' . $selected_ . '>' . >> label_ .. '</option>' . "\n"; >> >> >> } >> >> $content .= '</select>'; >> return $content; >> >> } >> >> >> } // end class >> >> >> >> You'd call it like this, I think: >> >> $form_widget = new FormWidgets; >> >> $popup_menu = >> $form_widget->makePopupMenu('name'=>'some_name','values'=>array ('item >> 1','item 2'),'labels'=>array(...)...); > > Well, I kinda see how you want to do that, but IMHO you are not taking > advantage of the language, > as you are trying to do it PERL way... I only know the perl way (actually that was lifted from javascript). I'm trying to learn the php way. Aside from that, I'm believer in templates and that the core html should be separate from the programming. I'm a couple days into learning php and I'd use the right style if I knew it! However there is one error that I > can spot on your class declaration > -- Constructor will always return an instance of the class no matter > what you return Thanks. I see now that my constructor is useless. It took me a long time to figure out that I needed a closing semicolon, that a "}" did not negate it's need. > >> > -- SNIP -- >> >> >> Hmmm. I normally have a small configure file for each server which >> has a database handle and paths in it. Can I just name a file with the >> pdo extension and will php use it (if it's in the library path) >> without have to call include or require? Should I be using pdo for my >> "module" extension? Those "modules" will just have classes. > > No, you need to use "include" or "require"... OK. I guess require_once... > Yes, you can use PDO to access database, as it provides abstraction > layer to access database. > There are some people who does not like to use it, citing performance > issues. Hmmm. I'm more concerned about my performance benefit! I understand PDO and my guess is that once you create a database handle that you're over the hump. > >> > -- SNIP -- >> >> Do you know if the the include_once is like the perl 'do'? > > Hmmm, PHP takes a more simplistic approach to it's include_once. > Anything that you tell PHP under include_once directive will simply > means to include the > specified files to be executed too... Please refer to PHP manual for > more info. I'm trying. Sometimes it is damn hard to find it in the index. Jeff > > Hendri Kurniawan |
|
|||
|
Jeff wrote:
> -- SNIP -- > I only know the perl way (actually that was lifted from javascript). I'm > trying to learn the php way. Aside from that, I'm believer in templates > and that the core html should be separate from the programming. I'm a > couple days into learning php and I'd use the right style if I knew it! > Then you'll love Smarty (http://www.smarty.net/)... I can't think of any other templating engine (others in the NG might now)... I don't use it myself, because: 1/ Smarty is OLD -- It uses PHP4 2/ It has quite an overhead that I don't require But I do base my templating engine based on Smarty I'm pointing Smarty out to you, as it can provide a basis for your templating, and as the template is partly plain HTML, web designer ca easily read them (with some learning of course) What Smarty does: $countries = array( 'au' => 'Australia', 'us' => 'United States', 'uk' => 'United Kingdom' ); $tpl = new Smarty(); $tpl->assign('SelectCountries', $countries); $tpl->display('select_country_form.tpl'); > -- SNIP -- > Hmmm. I'm more concerned about my performance benefit! I understand PDO > and my guess is that once you create a database handle that you're over > the hump. IMHO the performance is negligible compared to the benefit or abstracting your database layer and by the fact that PDO ships with PHP (5.1 onwards), means it will quickly become the standard (IMHO) > -- SNIP -- Hendri Kurniawan |