Solved

Object Oriented PHP

Posted on 2004-07-31
2
219 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 75 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now