Jump to content
Science Forums

Php


moon

Recommended Posts

hi Tormod,

 

thanks for your reply..........

 

Are you familiar with php programming? Now i'm doing a data mining project using php code. I use the OOP concept in php4. However, OOP in php5 seem more powerful.

 

Before this, i code program with php4 but seem like have some problems when i need to apply the OOP in it. I'm not so familiar with php actually...... i'm learning and do the program at the same time....

 

The problem that i face such as returning an object in a function.it seem like cant work in php4. do u have any idea about it? or may be i'm wrong..........

 

thanks...

Link to comment
Share on other sites

hi Tormod,

 

thanks for your reply..........

 

Are you familiar with php programming? Now i'm doing a data mining project using php code. I use the OOP concept in php4. However, OOP in php5 seem more powerful.

Moon, I am fairly new to php myself. I have worked with ColdFusion since 1997 but have started to pick up php because we switched forum software here at Hypography back in November.

 

Alexander, our computer/tech moderator, is an experienced programmer and I am sure others here can help out, too. Some of our regulars are probably not around at the moment since it's so soon after the holidays but hopefully we'll see them soon.

 

On a theoretical level I am able to discuss programming, though, so feel free to ask and I'll try to help as much as I can!

Link to comment
Share on other sites

Welcome moon,

php4 OOP support wasnt the greatest to say the least, it was like a hat that didnt really fit, not to say that PHP4 wasn't awesome as PHP is.

as to what you're trying to do, PHP5 is pretty stable, there are patches that are released for it, but its pretty good as long as you have the latest version. If you want to go from php4 to 5 with OO code, you would have to edit it quite a bit. OO was rewritten, here's the overview and some explanation on how 5 is different from 4. http://www.sitepoint.com/article/coming-soon-webserver-near

anyways, you'd have to explain little more of what you are trying to do, but here's my first guess at resolving your problem:

function &find_var ($param)

{

   ...code...

   return $found_var;

}



$foo =& find_var ($bar);

$foo->x = 2; 

Link to comment
Share on other sites

thank you, alexander...........

 

as i know, php is quite similar to java......... i know java much more than php, and i also refer to java when code the php.

 

for me, there are quite big difference between php4 and php5 for the OO part.....

can you show me how to code the java in php way? here is the java file :

 

----------------------------------------------------------------------------------

public class Item {

private String item;

 

public Item(String item){

this.item = item;

}

public Item(Item item){

this.item = item.item;

}

public String getItem() {

return item;

}

public void setItem(String item) {

this.item = item;

}

public boolean equals(Object o) {

return item.equals(((Item)o).item); //casting

}

public int compareTo(Object o) {

return item.compareTo(((Item)o).item);

}

public String toString() {

return item;

}

 

public static void main(String args[]){

Item i1 = new Item("1");

Item i2 = new Item("1");

Item i3 = new Item("2");

System.out.println(i1);

System.out.println(i1.equals(i2));

System.out.println(i1.equals(i3));

}

}

--------------------------------------------------------------------------------------

 

if it is not convenient, it's ok...... thanks anyway............. :)

Link to comment
Share on other sites

i know java much more than php
I'm so sorry for you, noone should use Java, you might as well use C++ if you write Java code, it will execute about 5 times faster...

PHP5's OO model is completely different, lots of synthax is different, but its better!

i wont rewrite the code, i'll leave that to you, here's a few things i found that should help:

//An example of how to pass an object back into its own class for direct use.

//(thisClass.php)

<?php

class thisClass{

  var $var1;
 
  function thisClass($value)
      {$this->var1 = $value;}
     
  function set_var1($value)
      {$this->var1 = $value;}
  function get_var1()
      {return $this->var1;}
 
  function showVar()       
      {echo "<p>var1 = ".$this->var1."</p>";}
 
  function callShowVar($object)
      {$object->showVar();}
 
  function copyObject($object)
      {$this->var1 = $object->get_var1();}

}

?>

//(test.php)
<?php

require_once('class.php');

$thisObject = new thisClass(3);
$thatObject = new thisClass(1);

$thatObject->callShowVar($thisObject); //outputs "var1 = 3"

$thisObject->showVar(); //outputs "var1 = 3"
$thatObject->showVar(); //outputs "var1 = 1"
$thatObject->copyObject($thisObject);
$thatObject->showVar(); //outputs "var1 = 3"

?>

//It seems there is no way to access the return value of a method (or any function) inline, without assigning it to a variable.

//For example:

<?php
class Test
{
 function blah ()
 {
    return array(1,2,3);
 }

 function childTest ()
 {
    return new Test;
 }
}

$test = new Test;

// This does not work:
$foo = $test->blah()[0];

// Instead have to do:
$temp = $test->blah();
$foo = $temp[0];

// Similarly for objects, cannot do:
$foo = $test->childTest()->blah();

// Instead have to do:
$temp = $test->childTest();
$foo = $temp->blah();

?>

 

To emulate Java/C++ functionality of arguments that are objects, you just have to modify the given function's signature from this:

 

function foo(..., $object, ...)

 

to this:

function foo(..., &$object, ...)

 

The ampersand(&) thus signifies that the function should receive a reference to the object, instead of creating a copy of the object.

 

and lastly comparing two objects:

<?php
function bool2str($bool) {
  if ($bool === false) {
          return 'FALSE';
  } else {
          return 'TRUE';
  }
}

function compareObjects(&$o1, &$o2) {
  echo 'o1 == o2 : '.bool2str($o1 == $o2)."n";
  echo 'o1 != o2 : '.bool2str($o1 != $o2)."n";
  echo 'o1 === o2 : '.bool2str($o1 === $o2)."n";
  echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."n";
}

class Flag {
  var $flag;

  function Flag($flag=true) {
          $this->flag = $flag;
  }
}

class SwitchableFlag extends Flag {

  function turnOn() {
      $this->flag = true;
  }

  function turnOff() {
      $this->flag = false;
  }
}

$o = new Flag();
$p = new Flag(false);
$q = new Flag();

$r = new SwitchableFlag();

echo "Compare instances created with the same parametersn";
compareObjects($o, $q);

echo "nCompare instances created with different parametersn";
compareObjects($o, $p);

echo "nCompare an instance of a parent class with one from a subclassn";
compareObjects($o, $r);
?> 

//or if you want to compare compound objects:
<?php
class FlagSet {
  var $set;

  function FlagSet($flagArr = array()) {
      $this->set = $flagArr;
  }

  function addFlag($name, $flag) {
      $this->set[$name] = $flag;
  }

  function removeFlag($name) {
      if (array_key_exists($name, $this->set)) {
          unset($this->set[$name]);
      }
  }
}


$u = new FlagSet();
$u->addFlag('flag1', $o);
$u->addFlag('flag2', $p);
$v = new FlagSet(array('flag1'=>$q, 'flag2'=>$p));
$w = new FlagSet(array('flag1'=>$q));

echo "nComposite objects u(o,p) and v(q,p)n";
compareObjects($u, $v);

echo "nu(o,p) and w(q)n";
compareObjects($u, $w);
?> 

 

also note that this is all PHP 4 code, taken from www.php.net

Link to comment
Share on other sites

Well, as i said, i could write the code for you, but then it doesnt teach you anything about how to deal with this or similar situation next time, i hope you are not just being sorcastic and that code rally taught you something :)

Please, if you just cant figure it out and are giving up hope, its not a big deal, i have some time that i can spend rewriting that code for you, i dont want you to get discouraged from posting here or even worse to get discouraged to program in PHP or program at all in the future, and i dont want my first impresion to be bad, so if there are any questions, please, i will answer them straight and without sending you to any resource pages if you want...

Link to comment
Share on other sites

hi alexander,

 

it's ok for me to see the code myself.... :)

thanks for your offer......

 

anyway, i have some questions......in php, the symbol @ represents of what?

for example:

if ($db_link) @mysql_select_db(DB_DATABASE);

 

why use the '@'?

 

thanks

Link to comment
Share on other sites

i beleive that @ forces PHP to supress any errors resulting from the function call.

 

its a good idea to know when things go wrong, but if you have your own error reporting functions you'd want to use @, also if you need to execute something before the program bombs, you'd use help you exit propperly (such would be the case when working with files sometimes databases, sockets and so forth...)

Link to comment
Share on other sites

here's a manual on pear:

http://pear.php.net/manual/en/

 

but in short, pear is

" PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:

 

*

 

A structured library of open-sourced code for PHP users

*

 

A system for code distribution and package maintenance

*

 

A standard style for code written in PHP, specified here

*

 

The PHP Foundation Classes (PFC), see more below

*

 

The PHP Extension Community Library (PECL), see more below

*

 

A web site, mailing lists and download mirrors to support the PHP/PEAR community

 

PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then. "

the fact that you have a folder named pear is if you wanted to build in pear support for php that folder would be used, but if you remember php is distributed in an archive with all the files and folders if you use installers the unnecessary files would be removed i would think, my pear folder contains a php file and a folder with a whole bunch of archives.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...