By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,880 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,880 IT Pros & Developers. It's quick & easy.

Is general property class useful?

P: n/a
Hi all php freaks!

Do you think this kind of "property class" is useful or not? I have bee
bored to the way I've been coding earlier. because:

- often i have database-oriented classes like product, category, news etc.
- often i have fex. load, request, insert, update and delete functions for
them
- often problems are the same ones:
- which member variables are expected from the form, which ones are not
- which form fields are obligatory to fill , which ones are not
- which form fields go directly to database, which ones do not

So i was thinking that maybe "property class" could make this easier. But I
am still afraid of changing the current application to this ideology! What
do You think? Is my code gonna get messier and longer or cleaner and
shorter?

NOTE 1: "product class" is just a minimal example, don't think too much
about that.
NOTE 2: somebody possibly would dare to include property class into property
class (if that is even possible) but I am afraid of recursive things little
bit. Flags here are simple and safe sub-properties.

Perttu Pulkkinen, Finland

<?
class prop
{
var $value;
var $flags = array();

function prop($value=null)
{ $this->value = $value; }
function set($value)
{ $this->value = $value; }
function get()
{ return $this->value; }

function set_flag($flag, $value)
{ $this->flags[$flag] = $value; }
function unset_flag($flag)
{ if(isset($this->flags[$flag])) unset($this->flags[$flag]);}
function get_flag($flag)
{ if(isset($this->flags[$flag]))return $this->flags[$flag]; else return
null; }
function get_flags()
{ return $this->flags; }

}

class product
{
function product(&$msg, $name, $price=0)
{
$this->name = new prop($name);
$this->name->set($name);

$this->price = new prop();
$this->price->set_flag('min', 0);
$this->price->set_flag('max', 1000);

$this->set_price($msg, $price);
}
function set_price(&$msg, $price="0")
{
if(
$price >= $this->price->get_flag('min') &&
$price <= $this->price->get_flag('max')
)
{ $this->price->set($price); $msg=""; return true; }
else {$msg = "Not good!"; return false;}
}
}

// test program starts
$msg ="";
$x = new product($msg,'test');
echo $x->name->get();
echo $x->price->get();
$x->set_price($msg,800);
echo $x->price->get();
echo $msg;
// test program ends

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> NOTE 2: somebody possibly would dare to include property class into
property
class (if that is even possible) but I am afraid of recursive things little bit. Flags here are simple and safe sub-properties.


And why suproperties (flags) are necessary is because:

$databasefields = array('name', 'price', 'description')

foreach($this->$databasefields as $field)
{
if ( is_object($this->$field) && $this->$field->get_flag('request') ==
true && isset($_REQUEST['$field']) )
$this->$field->set($_REQUEST['$field']);
}

so, i can easily foreach, because "variablename" (now object) and its use
are connected together. I can invent new kinds of flags to different
property obejct when i need them.
Jul 17 '05 #2

P: n/a
I have already implemented a solution to this problem. Take a look at
http://www.tonymarston.co.uk/php-mys...seobjects.html

I have an abstract class which contains generic code for every database
table, and a subclass for each physical table which identifies the unique
characteristics of that table. This includes a $fieldspec array which
identifies the fields in the table, what type they are (string, number,
etc), what size they are, and whether they are required or not, which takes
care of all the basic validation.

If you follow the links you will come to an online demonstration, the code
for which you can download to run on your own PC.

--
Tony Marston

http://www.tonymarston.net

"Perttu Pulkkinen" <pe**************@co.jyu.fi> wrote in message
news:vZ**************@read3.inet.fi...
Hi all php freaks!

Do you think this kind of "property class" is useful or not? I have bee
bored to the way I've been coding earlier. because:

- often i have database-oriented classes like product, category, news etc.
- often i have fex. load, request, insert, update and delete functions for them
- often problems are the same ones:
- which member variables are expected from the form, which ones are not - which form fields are obligatory to fill , which ones are not
- which form fields go directly to database, which ones do not

So i was thinking that maybe "property class" could make this easier. But I am still afraid of changing the current application to this ideology! What
do You think? Is my code gonna get messier and longer or cleaner and
shorter?

NOTE 1: "product class" is just a minimal example, don't think too much
about that.
NOTE 2: somebody possibly would dare to include property class into property class (if that is even possible) but I am afraid of recursive things little bit. Flags here are simple and safe sub-properties.

Perttu Pulkkinen, Finland

<?
class prop
{
var $value;
var $flags = array();

function prop($value=null)
{ $this->value = $value; }
function set($value)
{ $this->value = $value; }
function get()
{ return $this->value; }

function set_flag($flag, $value)
{ $this->flags[$flag] = $value; }
function unset_flag($flag)
{ if(isset($this->flags[$flag])) unset($this->flags[$flag]);}
function get_flag($flag)
{ if(isset($this->flags[$flag]))return $this->flags[$flag]; else return
null; }
function get_flags()
{ return $this->flags; }

}

class product
{
function product(&$msg, $name, $price=0)
{
$this->name = new prop($name);
$this->name->set($name);

$this->price = new prop();
$this->price->set_flag('min', 0);
$this->price->set_flag('max', 1000);

$this->set_price($msg, $price);
}
function set_price(&$msg, $price="0")
{
if(
$price >= $this->price->get_flag('min') &&
$price <= $this->price->get_flag('max')
)
{ $this->price->set($price); $msg=""; return true; }
else {$msg = "Not good!"; return false;}
}
}

// test program starts
$msg ="";
$x = new product($msg,'test');
echo $x->name->get();
echo $x->price->get();
$x->set_price($msg,800);
echo $x->price->get();
echo $msg;
// test program ends

Jul 17 '05 #3

P: n/a
No, I don't think it's useful. This kind of solutions always fall apart when
you apply them to real-world problems, because there's always one thing that
doesn't fit within the grand scheme of things. What you end up with is a lot
of ugly workaround code.

Uzytkownik "Perttu Pulkkinen" <pe**************@co.jyu.fi> napisal w
wiadomosci news:vZ**************@read3.inet.fi...
Hi all php freaks!

Do you think this kind of "property class" is useful or not? I have bee
bored to the way I've been coding earlier. because:

- often i have database-oriented classes like product, category, news etc.
- often i have fex. load, request, insert, update and delete functions for them
- often problems are the same ones:
- which member variables are expected from the form, which ones are not - which form fields are obligatory to fill , which ones are not
- which form fields go directly to database, which ones do not

So i was thinking that maybe "property class" could make this easier. But I am still afraid of changing the current application to this ideology! What
do You think? Is my code gonna get messier and longer or cleaner and
shorter?

NOTE 1: "product class" is just a minimal example, don't think too much
about that.
NOTE 2: somebody possibly would dare to include property class into property class (if that is even possible) but I am afraid of recursive things little bit. Flags here are simple and safe sub-properties.

Perttu Pulkkinen, Finland

<?
class prop
{
var $value;
var $flags = array();

function prop($value=null)
{ $this->value = $value; }
function set($value)
{ $this->value = $value; }
function get()
{ return $this->value; }

function set_flag($flag, $value)
{ $this->flags[$flag] = $value; }
function unset_flag($flag)
{ if(isset($this->flags[$flag])) unset($this->flags[$flag]);}
function get_flag($flag)
{ if(isset($this->flags[$flag]))return $this->flags[$flag]; else return
null; }
function get_flags()
{ return $this->flags; }

}

class product
{
function product(&$msg, $name, $price=0)
{
$this->name = new prop($name);
$this->name->set($name);

$this->price = new prop();
$this->price->set_flag('min', 0);
$this->price->set_flag('max', 1000);

$this->set_price($msg, $price);
}
function set_price(&$msg, $price="0")
{
if(
$price >= $this->price->get_flag('min') &&
$price <= $this->price->get_flag('max')
)
{ $this->price->set($price); $msg=""; return true; }
else {$msg = "Not good!"; return false;}
}
}

// test program starts
$msg ="";
$x = new product($msg,'test');
echo $x->name->get();
echo $x->price->get();
$x->set_price($msg,800);
echo $x->price->get();
echo $msg;
// test program ends

Jul 17 '05 #4

P: n/a
Hi Perttu,

Is this like what you mean:
http://www.phppeanuts.org/site/index...escriptor.html
Source code:
http://www.phppeanuts.org/site/index...ertyDescriptor

Then yes, i think it can be made usefull. I am currently developing a
webbased bookkeeping app with it, i am about half way and spent 18 hours
yet :-) . But i admit, the property classes are just a foundation, i
think the user interface classes that are built on top that are the big
timesavers.

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.
Perttu Pulkkinen wrote:
Hi all php freaks!

Do you think this kind of "property class" is useful or not? I have bee
bored to the way I've been coding earlier. because:

- often i have database-oriented classes like product, category, news etc.
- often i have fex. load, request, insert, update and delete functions for
them
- often problems are the same ones:
- which member variables are expected from the form, which ones are not
- which form fields are obligatory to fill , which ones are not
- which form fields go directly to database, which ones do not

So i was thinking that maybe "property class" could make this easier. But I
am still afraid of changing the current application to this ideology! What
do You think? Is my code gonna get messier and longer or cleaner and
shorter?

NOTE 1: "product class" is just a minimal example, don't think too much
about that.
NOTE 2: somebody possibly would dare to include property class into property
class (if that is even possible) but I am afraid of recursive things little
bit. Flags here are simple and safe sub-properties.

Perttu Pulkkinen, Finland

<?
class prop
{
var $value;
var $flags = array();

function prop($value=null)
{ $this->value = $value; }
function set($value)
{ $this->value = $value; }
function get()
{ return $this->value; }

function set_flag($flag, $value)
{ $this->flags[$flag] = $value; }
function unset_flag($flag)
{ if(isset($this->flags[$flag])) unset($this->flags[$flag]);}
function get_flag($flag)
{ if(isset($this->flags[$flag]))return $this->flags[$flag]; else return
null; }
function get_flags()
{ return $this->flags; }

}

class product
{
function product(&$msg, $name, $price=0)
{
$this->name = new prop($name);
$this->name->set($name);

$this->price = new prop();
$this->price->set_flag('min', 0);
$this->price->set_flag('max', 1000);

$this->set_price($msg, $price);
}
function set_price(&$msg, $price="0")
{
if(
$price >= $this->price->get_flag('min') &&
$price <= $this->price->get_flag('max')
)
{ $this->price->set($price); $msg=""; return true; }
else {$msg = "Not good!"; return false;}
}
}

// test program starts
$msg ="";
$x = new product($msg,'test');
echo $x->name->get();
echo $x->price->get();
$x->set_price($msg,800);
echo $x->price->get();
echo $msg;
// test program ends


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.