Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Object Oriented PHP

Posted on 2004-07-31
2
Medium Priority
?
229 Views
Last Modified: 2009-07-29
I'm trying to convert some Java code into PHP code:

I created a class called Product in PHP, and a class called ProductDB which has utility functions for the database.  The java code is:

Product product = ProductDB.readRecord(connection, productCode);

Are there static methods in PHP?  Would I have to do something like the following?

$product = new Product();
$productdb = new ProductDB();
$product = $productdb->readRecord($connection, $productCode);

Also how do user defined objects work with sessions ( adding/retrieving) ?
0
Comment
Question by:abstractionz
2 Comments
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 11687051
Hi,

Ans 1) there are no static methods till php version 4.3
Ans 2) if you want some function of your class to return an object of other class just do it like the following code

//Start of some [ test_script.php ]
//~~~~~~~~~~~~~
require_once("product.php"); //this file contains the product related logic
require_once("productDB.php"); //this file has the database logic related to products

//you see the product file needs to be include/required first/before the productDB.php file becos this will help the productDB.php file to return an object variable through it's member function

Now your code's turn

//$product = new Product(); //no need of this your $product variable will be instentiated and will be valid when returned
$productdb = new ProductDB();
$product = $productdb->readRecord($connection, $productCode);

//~~~~~~~~~~~~~~~
//Start of some [ test_script.php ]

now in your member function [readRecord] of productDB class
you will have to create a new object of product class and set it's properties by code
like this before return from the function

$record = $productdb->readRecord($connection, $productCode);
//if $record = array then

$temp_prod = new Product();
$temp_prod->name = $record["name"]
$temp_prod->id = $record["id"];

//and so on to the other properties that you get from $record object

Hope  this helped...
Regards,
Meet
0
 
LVL 2

Accepted Solution

by:
d_tan earned 300 total points
ID: 11690243
I would highly suggest going to PHP5 if you are converting from JAVA.  This is because, in PHP5 the object model has changed significantly to use object references (like JAVA).  Likewise, there are a ton of new features which make the new version look a lot like (and feel like JAVA) as well.  

Furthermore your code:

Product product = ProductDB.readRecord(connection, productCode);

would be identical if you declare readRecord a static function in php 5.  Also, user defined objects can be easily added to the session.  Same as usual . . . $_SESSION['myObject'] = $myObect . . . .same for retriveing.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question