Forum Moderators: coopster

Message Too Old, No Replies

object oriented php

how to locate oop functions

         

nyteshade

6:05 pm on Sep 28, 2011 (gmt 0)

10+ Year Member



I just finished updating my javascript pages to js using dom scripting. I used that object knowledge to update mysql to mysqli extensions. Since I was having so much fun with my new friend objects I decided to update a dozen or so procedural php pages to oo php. My question is how do I recognize what functions have a oo php counterpart? The mysql conversion was a snap because the PHP manual has them on a single page. But for instance, I just converted a few lines to this:

...<select name='author'>";
while ($row = $result->fetch_assoc()) {
extract($row);
echo "<option value='$author'>$author</option>\n";
}
echo "</select>...


Is there an object oriented 'extract' function? Should I even be using extract (it was in my procedural code but it still works here)? And, what about other functions like (string functions) htmlentities for example and such. I'm looking thru the manual and don't see a oo function for it. Thanks all.

Readie

8:27 pm on Sep 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[uk.php.net...]

while($row = $result->fetch_object()) {
echo $row->columnName;
}


:)

Also: Extract is nasty :P So easy to accidentally screw up your code and override existing variables in ways you don't want to.

penders

12:44 am on Sep 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PHP is really a procedural language with an OOP 'bolt-on', so the vast majority of the core functions are procedural and I can't see that changing any time soon. There is no string object so all string functions are procedural.

...how do I recognize what functions have a oo php counterpart?


I'm not aware of any definitive list. MySql to mysqli is an obvious one. But mysqli offers a lot more than just the OOP interface. mysqli can also be used entirely procedurally.

A few others that spring to mind... the Standard PHP Library (SPL) contains classes for file handling eg. SplFileObject and Iterators such as the DirectoryIterator for stepping through directories instead of opendir() etc. The DateTime classes offer many improvements over the standard procedural functions, but unfortunately many (of the better) methods are not available until PHP 5.3.

What others are there?

penders

12:50 am on Sep 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Regarding the use of extract()... as Readie says, a bit nasty, but it's also completely unnecessary in the code you have posted, since you can refer to $row['author'].

nyteshade

2:04 am on Sep 29, 2011 (gmt 0)

10+ Year Member



Thanks Readie, Penders: good stuff.

Readie

7:47 pm on Sep 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What others are there?

PDO is one I've used before - is for databases, so you can jump between different types of SQL database without modifying the code. Quite irritating to work with at times though.

nyteshade

1:22 pm on Sep 30, 2011 (gmt 0)

10+ Year Member



I tried both Readie's fetch_object and pender's associated array examples, both work with an object so I assuming that the object results are the same, thanks again:
<select name='author'>";
$sql= "select distinct author from classics order by author";
$result= $mysqli->query($sql) or die ("Unable to execute query.");
while ($row = $result->fetch_object()) {
echo "<option value='". $row->author ."'>".$row->author ."</option>";
}
echo "</select></li></ul>

and

$sql= "select distinct author from classics order by author";
$result= $mysqli->query($sql) or die ("Unable to execute query.");
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row['author']."'>".$row['author']."</option>";
}
echo "</select></li></ul>

enigma1

3:09 pm on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When correctly used, extract can be an efficient mechanism to pull object references. For instance instead of having multiple lines to pull in helper object refs, for a member function in a class you could use extract with a loader and the necessary helpers become available with one line of code.

But for the example above pulling some records from the db surely is not efficient.

penders

3:51 pm on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@enigma1 Sounds interesting, do you have an example?

enigma1

4:51 pm on Oct 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes I have developed a CMS which implements this.
[sourceforge.net...]

The code that performs this is a general function


function &tep_load() {
static $_objects = array();
$result_array = array();
$args = func_get_args();
if( empty($args) ) return $_objects;

foreach( $args as $name ) {
$dir = DIR_FS_CLASSES . $name;
$file = $dir . '.php';

if( !isset($_objects[$name]) ) {
include_once($file);

if( !class_exists($name) ) {
die('Critical: Invalid Class File: ' . $file);
}
$_objects[$name] = new $name;
}
$result_array[$name] =& $_objects[$name];
}
return $result_array;
}


Then when I need to use it in a member function:


class install_download_system extends plug_manager {
....
function process_options() {
extract(tep_load('defs', 'database'));
...
$plugins_query = $db->query("select plugins_data from " . TABLE_PLUGINS . " where plugins_key = '" . $db->filter($this->key) . "'");
$plugins_array = $db->fetch_array($plugins_query);
...
}
}


The sample I posted is not complete by any means and I omitted the cross-ref part. It is coded so it's compatible with PHP 4 and 5.