Solved

Object Oriented PHP

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

785 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