473,499 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is general property class useful?

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
4 1849
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1474
by: Andrew MacLean | last post by:
Hello all, I am fairly new to .NET, but not to VB. I have a couple of questions. 1. Shared Code Base I have a solution with several different applications within it. Many of the...
1
3241
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
4
1983
by: Mike | last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am trying to implement a singleton class. Form 1 should create a user object and populate some properties in user. Form2...
1
1792
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference...
6
9033
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
27
1823
by: Just Me | last post by:
I made a Usercontrol that must have AutoScroll set to true when it is used. So I set it to True in the Load event. However the property still shows in the properties window when the control is...
9
8854
by: Stefan De Schepper | last post by:
Should I use: Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value
3
1184
by: Nick | last post by:
Hello, This isn't specific to C#, but that is what I am using to develop my app. I am trying to figure out how to organize my code and I'm not sure what the best way of doing it is. For the sake...
2
2184
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.