php-general Digest 28 Jan 2007 11:55:34 -0000 Issue 4594

Topics (messages 247892 through 247906):

Re: Create ACH Origination file with PHP
        247892 by: Jay Blanchard

My objects eats too much (and weird!) memory
        247893 by: Francisco M. Marzoa Alonso
        247894 by: Roman Neuhauser
        247896 by: Francisco M. Marzoa Alonso
        247897 by: Colin Guthrie
        247901 by: Francisco M. Marzoa Alonso

Re: SQL Readability..  (was Re: most powerful php editor)
        247895 by: Jochem Maas
        247902 by: Larry Garfield
        247906 by: Jochem Maas

Re: Can a class instance a property of another class
        247898 by: Ken Kixmoeller -- reply to ken.kixmoeller.com
        247899 by: Myron Turner
        247900 by: Jochem Maas

DATE
        247903 by: Ron Piggott
        247904 by: Casey Chu
        247905 by: Francisco M. Marzoa Alonso

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
[snip]
I am familiar with the NACHA file format, and have the specifications, I
just would rather not write the file formatter and output if someone
else
has done it already or could do it more easily than I.
Do you have experience in this? 
[/snip]

Ah. Yes I do have experience in this and have done it several times over
the years. I have never made the code public typically because I would
have to edit sensitive information out of it.

Here is an example, edited with comments about where stuff should occur,
since this has been edited it has not been tested and may require
significant modification to work in any case (it includes being able to
keep a log); 

/*
  * includes
  */
include("your database connection");
/* 
 * create the ACH transfer request records to be transmitted
 * each batch must contain
 *              file header record
 *              company/batch header record
 *              detail record(s)
 *                      addenda record(s) OPTIONAL
 *              company/batch control record
 *              file control record
 * 
 * all alphanumeric and alpha characters must be upper case with fields
left justified, and blank filled
 * all numeric fields must be right justified, unsigned, and zero filled
 * 
 * detail records will be created first so that appropriate details can
be gathered for header and control records
 * 
 */  
  
 /* 
  * entry detail records
  *     this is requires a loop
  *     we will loop through each dealer's daily records for the day in
question and sum the dollar amount that they owe us.
  *     we will have one detail record for each dealer each day that has
a total > 0
  *     we will then get their banking info and create the detail record
  *     if there is no banking info we will not run a detail record
  */
  
  $getDealerAcct = "your query for account info ";
  if(!($dbDealerAcct = mysql_query($getDealerAcct, $dbc))){
                echo mysql_error();
                exit();
  }
  /* get number of rows */
  if(0 != mysql_num)
  /* loop through detail records */
 
 $detailRecCount        = 0; //initialize record count
 $detailHash            = 0; //initialize detail hash
 $totBatchDollar        = 0; //initialize batch dollars
 $totBlockCount         = 0; //initailize block count
 $totEntryRecord        = 0; //initailize block count
 $dollar = ''; // initialize amount
 $startDate = date("Y-m-d", strtotime("-2days")); //The start date
should be two days ago
 if($_GET['date']!=''){
        $startDate = $_GET['date'];
 }

 $endDate = $startDate; //The end date should be the same as the start
date
 
 $txtLog = "Dealer Code\tDealer Name\tAmount\n"; // header code for log
file
 $logLine = ""; //initializing line
 
 while($dealerAcct = mysql_fetch_array($dbDealerAcct)){
 
 //20060906jc  Here let's find the correct dollar amount for what we are
looking for
 $dealerCode = $dealerAcct['dealerCode'];
 $logLine.= $dealerCode."\t";
 $sqlGetName = "get account name";
 if(!($dbGetName = mysql_query($sqlGetName, $dbc))){
        echo "Error Getting
Name!(".mysql_errno()."):".mysql_error()."<br />\n";
        echo $sqlGetName."<br />\n";
        exit();
 }
 $arrGetName =  mysql_fetch_assoc($dbGetName);
 $dealerName = $arrGetName['dealerDBA'];
 if($dealerName==""){
        $dealerName="[none given]";
 }
 $logLine.= $dealerName."\t";


 $sqlGetMoola = "query to get amount associated with account ";
 if(!($dbGetMoola = mysql_query($sqlGetMoola, $dbc))){
        echo "Error Getting tha
Moola!(".mysql_errno()."):".mysql_error()."<br />\n";
        echo $sqlGetMoola."<br />\n";
        exit();
 }
 $dollar = 0;
 $index = 0;
 $intGetMoola = mysql_num_rows($dbGetMoola);
 while($index < $intGetMoola){
        $arrGetMoola = mysql_fetch_array($dbGetMoola);
        $dollar = $dollar + $arrGetMoola['amount'];
        $index++; 
 }
 $logLine.= $dollar."\n";
 //echo $logLine;
 if($dollar!=0){
        $txtLog.=$logLine;
 }
 $logLine = "";
 $dollar = $dollar * 100;
 
 if($dollar != 0){
        $detailRecord .=        '6';
        $detailRecord .=        '27'; // transaction code
        $detailRecord .=        substr($dealerAcct['dealerABARoute'], 0,
8); // ABA routing number (first eight characters)
        $detailRecord .=        substr($dealerAcct['dealerABARoute'], 8,
1); // routing number check digit (ninth character in routing number)
        $detailRecord .=        str_pad($dealerAcct['dealerABAAccount'],
17, " ", STR_PAD_RIGHT); // account number, left justify blank fill
        $detailRecord .=        str_pad($dollar, 10, "0", STR_PAD_LEFT);
// amount
        $detailRecord .=        str_pad('', 15, " ", STR_PAD_RIGHT); //
individual id number
        $detailRecord .=
str_pad(substr(strtoupper($dealerAcct['dealerDBA']), 0, 22), 22, " ",
STR_PAD_RIGHT); // individual name
        $detailRecord .=        str_pad('', 2, " ", STR_PAD_RIGHT); //
BoA draft indicator
        $detailRecord .=        '0'; // addenda indicator
        $detailRecord .=        str_pad('', 15, "0", STR_PAD_LEFT);
//Trace Number
        $detailRecord .=        "\n"; // end of line
        $detailRecCount++; 
        $detailHash     =       $detailHash +
substr($dealerAcct['dealerABARoute'], 0, 8);
        $totBatchDollar =       $totBatchDollar + $dollar;
        $totBlockCount++;
        $totEntryRecord++;
 }
  } /* end detail record loop */
 
 /* file header record */
 $fileHeader  =         ''; // these fields as required
 $fileHeader .=         '';
 $fileHeader .=         ''; // immediate destination
 $fileHeader .=         ''; // immediate origin
 $fileHeader .=         date("ymd");
 $fileHeader .=         date("Hi");
 $fileHeader .=         'A'; // we may need to sequence this if more
than one is sent per day
 $fileHeader .=         '';
 $fileHeader .=         '10'; // number of records per block???
 $fileHeader .=         '1';
 $fileHeader .=         str_pad('BANK NAME', 23, " ", STR_PAD_RIGHT); //
destination
 $fileHeader .=         str_pad('ORIGIN NAME', 23, " ", STR_PAD_RIGHT);
// origin
 $fileHeader .=         str_pad('', 8, " "); // reference code
 $totBlockCount++;
 
 /* company/batch header record */
 $batchHeader  =        '';
 $batchHeader .=        ''; // service class code
 $batchHeader .=        '';
 $batchHeader .=        str_pad('', 20, " ", STR_PAD_RIGHT); //
discretionary data
 $batchHeader .=        '';
 $batchHeader .=        '';
 $batchHeader .=        '';
 $batchHeader .=        date("ymd");
 $batchHeader .=        date("ymd", strtotime("tomorrow"));
 $batchHeader .=        str_pad('', 3, " ", STR_PAD_RIGHT); //
settlement date
 $batchHeader .=        '';
 $batchHeader .=        '';
 $batchHeader .=        ''; //batch sequence
 $totBlockCount++;
 
 /* company/batch control record */
 $batchControl  =       '';
 $batchControl .=       '';
 $batchControl .=       str_pad($detailRecCount, 6, "0", STR_PAD_LEFT);
// entry + addneda record count
 $batchControl .=       str_pad(substr($detailHash, -10, 10), 10, "0",
STR_PAD_LEFT);// entry hash - sum of ABA routing number 8 digits, length
of 10, drop 2 left most digits
 $batchControl .=       str_pad($totBatchDollar, 12, "0", STR_PAD_LEFT);
// total dollar amount, no decimal point
 $batchControl .=       str_pad('', 12, "0", STR_PAD_LEFT); // total
credit amount, no decimal point
 $batchControl .=       '';
 $batchControl .=       str_pad('', 19, " ", STR_PAD_RIGHT); // message
authentication code
 $batchControl .=       str_pad('', 6, " ", STR_PAD_RIGHT); // reserved
 $batchControl .=       ''; // originating DFI
 $batchControl .=       ''; //batch sequence
 $totBlockCount++;
 
 /* file control record */
 $totBlockCount++;
 $fileControl  =        '';
 $fileControl .=        str_pad('', 6, "0", STR_PAD_LEFT); // total
batches
 $fileControl .=        str_pad($totBlockCount, 6, "0", STR_PAD_LEFT);
// block count including header/trailer
 $fileControl .=        str_pad($totEntryRecord, 8, "0", STR_PAD_LEFT);
// entry + addneda record count
 $fileControl .=        str_pad(substr($detailHash, -10, 10), 10, "0",
STR_PAD_LEFT);// entry hash - sum of ABA routing number 8 digits, length
of 10, drop 2 left most digits
 $fileControl .=        str_pad($totBatchDollar, 12, "0", STR_PAD_LEFT);
// total dollar amount, no decimal point
 $fileControl .=        str_pad('', 12, "0", STR_PAD_LEFT); // total
credit amount, no decimal point
 $fileControl .=        str_pad('', 39, " ", STR_PAD_RIGHT); // reserved
 
 /*
  * use for testing 
 echo "<pre>\n";
 echo $fileHeader . "\n";
 echo $batchHeader . "\n";
 echo $detailRecord; // EOL in each record
 echo $batchControl . "\n";
 echo $fileControl . "\n";
 echo "</pre>\n";*/
 
 $filecontents = $fileHeader . "\n";
 $filecontents.= $batchHeader . "\n";
 $filecontents.= $detailRecord; // EOL in each record
 $filecontents.= $batchControl . "\n";
 $filecontents.= $fileControl . "\n";
 
 if(!($filePtr = fopen("save a copy of the ACH file".$startDate.".ACH",
"w"))){
        echo "Error opening file (".$startDate.".ACH)\n";
        exit();
 }
 
 fwrite($filePtr, $filecontents);
 fclose($filePtr);

if(!($filePtr = fopen("a file that can be opened in
Excel".$startDate.".xls", "w"))){
        echo "Error opening file (".date("Ymd").".xls)\n";
        exit();
 }
 
 fwrite($filePtr, $txtLog);
 fclose($filePtr);

I hope this gets you pointed in the right direction, data calls will
have to be made and reference to those calls will have to be changed.
You probably do not need some of the queries here and can probably
simplify a couple of things, but this code comes from a production
system in use every day. 

P.S. always reply to the list in the event someone's SPAM filter sends
your individual e-mail to the SPAM bucket. Also, others may be able to
help as your describe your problems more.

--- End Message ---
--- Begin Message ---
Hello!

I'm new here, so I do not know if attachments are allowed, so I put my
code below.

I've written this to check memory consumption of PHP5 objects, because
I'm having memory problems with an OO XMLParser that I've written.

When I execute the code from cli, I get:



$ php MemTest.php
Obj1 uses 208 bytes
Obj2 uses 168 bytes
Obj3 uses 200 bytes
Obj4 uses 200 bytes
Obj5 uses 200 bytes
Obj6 uses 200 bytes


I understand that first instance may be bigger than next ones because
method allocation and other things of that kind, but I do not understand
why second instance is smaller than third :-?

So, my first question is: Why third object is a 16% smaller than third
and nexts?

In any case, my second question is: Isn't too much memory 200 bytes for
a so simply object??


And the third: Is there any manner to get more compact objects in PHP5?


Thanks a lot in advance.

-------
<?php

class dummy {
        protected $a;

        public function __construct ( $a ) {
                $this->a = $a;
        }
}

$before=0;
$memdiff=0;

$before = memory_get_usage();
$obj1 = new dummy (0);
$memdiff = memory_get_usage()-$before;
echo "Obj1 uses $memdiff bytes\n";

$before = memory_get_usage();
$obj2 = new dummy (0);
$memdiff = memory_get_usage()-$before;
echo "Obj2 uses $memdiff bytes\n";

$before = memory_get_usage();
$obj3 = new dummy (0);
$memdiff = memory_get_usage()-$before;
echo "Obj3 uses $memdiff bytes\n";

$before = memory_get_usage();
$obj4 = new dummy (0);
$memdiff = memory_get_usage()-$before;
echo "Obj4 uses $memdiff bytes\n";

$before = memory_get_usage();
$obj5 = new dummy (0);
$memdiff = memory_get_usage()-$before;
echo "Obj5 uses $memdiff bytes\n";

$before = memory_get_usage();
$obj6 = new dummy (0);
$memdiff = memory_get_usage()-$before;
echo "Obj6 uses $memdiff bytes\n";


?>

--- End Message ---
--- Begin Message ---
# [EMAIL PROTECTED] / 2007-01-26 20:40:34 +0100:
> I've written this to check memory consumption of PHP5 objects, because
> I'm having memory problems with an OO XMLParser that I've written.

It measures something else though. The memory manager doesn't allocate
memory for individual variables in PHP.

> $ php MemTest.php
> Obj1 uses 208 bytes
> Obj2 uses 168 bytes
> Obj3 uses 200 bytes
> Obj4 uses 200 bytes
> Obj5 uses 200 bytes
> Obj6 uses 200 bytes
> 
> 
> I understand that first instance may be bigger than next ones because
> method allocation and other things of that kind

No.

> but I do not understand why second instance is smaller than third :-?

It's not.
 
> In any case, my second question is: Isn't too much memory 200 bytes for
> a so simply object??

It's not memory consumption of that object.

> And the third: Is there any manner to get more compact objects in PHP5?

You need to measure something before you want to compare it ("more
compact" than what?)


> class dummy {
>       protected $a;
> 
>       public function __construct ( $a ) {
>               $this->a = $a;
>       }
> }
> 
> $before=0;
> $memdiff=0;
> 
> $before = memory_get_usage();

What's $before here?

> $obj1 = new dummy (0);
> $memdiff = memory_get_usage()-$before;
> echo "Obj1 uses $memdiff bytes\n";
> 
> $before = memory_get_usage();

What's $before here?

> $obj2 = new dummy (0);
> $memdiff = memory_get_usage()-$before;
> echo "Obj2 uses $memdiff bytes\n";

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

--- End Message ---
--- Begin Message ---
On sáb, 2007-01-27 at 20:05 +0000, Roman Neuhauser wrote:
> # [EMAIL PROTECTED] / 2007-01-26 20:40:34 +0100:
> > I've written this to check memory consumption of PHP5 objects, because
> > I'm having memory problems with an OO XMLParser that I've written.
> 
> It measures something else though. The memory manager doesn't allocate
> memory for individual variables in PHP.
> 
> > $ php MemTest.php
> > Obj1 uses 208 bytes
> > Obj2 uses 168 bytes
> > Obj3 uses 200 bytes
> > Obj4 uses 200 bytes
> > Obj5 uses 200 bytes
> > Obj6 uses 200 bytes
> > 
> > 
> > I understand that first instance may be bigger than next ones because
> > method allocation and other things of that kind
> 
> No.
> 
> > but I do not understand why second instance is smaller than third :-?
> 
> It's not.
>  
> > In any case, my second question is: Isn't too much memory 200 bytes for
> > a so simply object??
> 
> It's not memory consumption of that object.


AFAIK there's no other way to measure memory usage on PHP than this, if
you know something better it could be more constructive if you show it.

> 
> > And the third: Is there any manner to get more compact objects in PHP5?
> 
> You need to measure something before you want to compare it ("more
> compact" than what?)

If I say that I need a more compact car I think everyone understands me
without needing to say "than the one I own", so I think its pretty clear
what I'm asking for.
 
> > $before = memory_get_usage();
> 
> What's $before here?

Memory consumption until next code.

> 
> > $obj1 = new dummy (0);
> > $memdiff = memory_get_usage()-$before;
> > echo "Obj1 uses $memdiff bytes\n";
> > 
> > $before = memory_get_usage();
> 
> What's $before here?

The same as $before of before.

Thank you by your helpless comment.

Attachment: signature.asc
Description: This is a digitally signed message part


--- End Message ---
--- Begin Message ---
Francisco M. Marzoa Alonso wrote:
> AFAIK there's no other way to measure memory usage on PHP than this, if
> you know something better it could be more constructive if you show it.

The last time I looked I think the xdebug module allowed you to get the
memory consumption of a given object in PHP. I can't remember for sure
tho', so check the docs/API before spending too much time with it :)

HTH

Col.

--- End Message ---
--- Begin Message ---
Hello,

I took a look to www.xdebug.org and the only memory related functions
was those I found here:

http://www.xdebug.org/docs-functions.php#tracing

int xdebug_memory_usage()
int xdebug_peak_memory_usage() 

The first seems to be the same that memory_get_usage 

http://es2.php.net/manual/en/function.memory-get-usage.php

that I'm using on my script, and the second seems to be just like
memory_get_peak_usage:

http://es2.php.net/manual/en/function.memory-get-peak-usage.php

That has no sense for this test.

Thanks anyway.

On sáb, 2007-01-27 at 20:46 +0000, Colin Guthrie wrote:
> Francisco M. Marzoa Alonso wrote:
> > AFAIK there's no other way to measure memory usage on PHP than this, if
> > you know something better it could be more constructive if you show it.
> 
> The last time I looked I think the xdebug module allowed you to get the
> memory consumption of a given object in PHP. I can't remember for sure
> tho', so check the docs/API before spending too much time with it :)
> 
> HTH
> 
> Col.
> 

Attachment: signature.asc
Description: This is a digitally signed message part


--- End Message ---
--- Begin Message ---
Larry Garfield wrote:
> On Saturday 27 January 2007 7:43 am, Jochem Maas wrote:
>> Larry Garfield wrote:
>>> I have long since given up on raw insert/update/delete statements as the
>>> syntax is all kinds nasty.  These days I just do this, which is even
>>> easier and more powerful:
>>>
>>> http://www.garfieldtech.com/blog/simplifying-sql
>> a quick look at those funcs gives me the impression that they are woefully
>> inadequate for any level of complex realworld use.
> 
> That's interesting, because I've been using variants of that for a year now 
> with much success in a dozen projects.  

I was nitpicking - I'm quite sure they are useful within the bounds of the
intended scope and wielded by a pair of hands that knows the code intimately
(including any limitations).

I run plenty of stuff that falls in the same category :-)

> 
>> query builders are alot more fiddly to get 'right' than one might imagine,
>> dealing with NULLs, booleans and dates for example (as Satyam pointed out)
>> can be a right PITA.
> 
> I actually almost never use native date types in the SQL database.  I just 
> store unix timestamps and do the math in PHP.  Dates are completely 
> unportable anyway.  I also tend to use ints for booleans, too, although 
> beefing up the switch statements in the code to handle native booleans should 
> be trivial.  

mysql doesn't have booleans does it? at least not versions I have to use.
with regard to date stuff, many people take the opposite approach and do most of
the date math inside SQL - most DBs have kickass date calculation functions btw.

and for the times when you need/want unix timestamps, mysql atleast, gives you
UNIX_TIMSTAMP().

(just some loose thoughts)

> 
>> perfect automated CRUD (it's an acronym!) is kind a holy grail - and
>> that is, I think, the driving force behind most attempts to crteate query
>> builders.
> 
> Orthogonal persistence is, yes.  The goal here was simply to make dealing 
> with 
> arbitrary insert and update statements easier, which in practice I've found 
> to be a huge success.  Full arbitrary CRUD and orthogonal persistence is much 
> harder.  That's why there's a dozen ORMs out there, all of which have some 
> major flaw. :-)  

including mine :-) (not released because it, well, needs a big manual that
only exists in my head - besides is firebird/ibase specific and I'm one of
about 5 people who actually use php+firebird :-)

> 
>> also I don't really agree with the sentiment that SQL syntax is nasty,
>> personally I find it, mostly, very easy to read and powerful ... but as
>> this thread shows there is no accounting for taste! :-)
> 
> What bugs me most about SQL syntax is INSERT vs. UPDATE.  I don't know the 
> underlying implementation details of the engine, but from the level I work at 
> (sending SQL to a database from a web app) I see no legitimate reason why 
> those two very-similar statements should have ridiculously different syntax.  

granted it's not perfect, somebody made a design 'fault' way back when and we're
stuck with it. maybe someone else has some real info about why this is so.

> 

--- End Message ---
--- Begin Message ---
On Saturday 27 January 2007 1:14 pm, Jochem Maas wrote:

> >> query builders are alot more fiddly to get 'right' than one might
> >> imagine, dealing with NULLs, booleans and dates for example (as Satyam
> >> pointed out) can be a right PITA.
> >
> > I actually almost never use native date types in the SQL database.  I
> > just store unix timestamps and do the math in PHP.  Dates are completely
> > unportable anyway.  I also tend to use ints for booleans, too, although
> > beefing up the switch statements in the code to handle native booleans
> > should be trivial.
>
> mysql doesn't have booleans does it? at least not versions I have to use.
> with regard to date stuff, many people take the opposite approach and do
> most of the date math inside SQL - most DBs have kickass date calculation
> functions btw.
>
> and for the times when you need/want unix timestamps, mysql atleast, gives
> you UNIX_TIMSTAMP().

At least as of MySQL 4.1 (haven't played with MySQL 5 much yet), yes, MySQL 
has no native boolean data type that I know of.  The standard alternative is 
TINYINT(1), which technically gives you values 0-9.  

And yes, I agree that MySQL has fairly decent date manipulation routines.  But 
at work we do try for database independence when possible, so except on 
specific projects we try to avoid it.

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

--- End Message ---
--- Begin Message ---
Larry Garfield wrote:
> On Saturday 27 January 2007 1:14 pm, Jochem Maas wrote:
> 
>>>> query builders are alot more fiddly to get 'right' than one might
>>>> imagine, dealing with NULLs, booleans and dates for example (as Satyam
>>>> pointed out) can be a right PITA.
>>> I actually almost never use native date types in the SQL database.  I
>>> just store unix timestamps and do the math in PHP.  Dates are completely
>>> unportable anyway.  I also tend to use ints for booleans, too, although
>>> beefing up the switch statements in the code to handle native booleans
>>> should be trivial.
>> mysql doesn't have booleans does it? at least not versions I have to use.
>> with regard to date stuff, many people take the opposite approach and do
>> most of the date math inside SQL - most DBs have kickass date calculation
>> functions btw.
>>
>> and for the times when you need/want unix timestamps, mysql atleast, gives
>> you UNIX_TIMSTAMP().
> 
> At least as of MySQL 4.1 (haven't played with MySQL 5 much yet), yes, MySQL 
> has no native boolean data type that I know of.  The standard alternative is 
> TINYINT(1), which technically gives you values 0-9.  
> 
> And yes, I agree that MySQL has fairly decent date manipulation routines.  
> But 
> at work we do try for database independence when possible, so except on 
> specific projects we try to avoid it.

again we differ :-) I have never bought the 'data independence' story - in 
practice
it's of little value imho most of the time (granted certain products do benefit 
- but
what I build doesn't fall into that category) and I find it crazy to end up with
a situation where the most advanced peice of data manipulation software in a 
given stack
is dumbed down to the lowest common denominator [of DB engines]. On more 
complex project
I try to cram as much of the data intregity and business logic in to the 
database itself
(for which I use firebird mostly) because it means being able to create 
different clients
to the data without replicating [as much] business logic (e.g. website and 
desktop app).
besides which the required stored procedures and triggers are usually hundreds 
of lines less
than their php equivalent AND more importantly they are intrinsically atomic 
(in the sense that
database transaction 'should' be).

rgds :-)

> 

--- End Message ---
--- Begin Message --- OK, Jochem, I adapted your example and got it working. Thank you very much.

I am still playing with it to better understand. One thing I don't yet understand is the necessity for the getFoo()/getBar() "handshake," especially the getbar() in the BAR class. That doesn't seem to serve any purpose. My adaptation us just a getDummy().

Do they just serve to pass the object by reference?


Ken

--------------------------------------------------------------
On Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:



class Foo
{
        private $var;
        function __construct() { $this->var = "foo"; }
        function getFoo() { return $this->var; }
}

class Bar
{
        private $var;
        private $foo;
        function __construct() { $this->var = "bar"; $this->foo = new Foo; }
        function getBar() { return $this->var; }
function speak() { echo "I am ",$this->foo->getFoo(),$this->getBar (),"\n"; }
}



--- End Message ---
--- Begin Message ---
Ken Kixmoeller -- reply to [EMAIL PROTECTED] wrote:
OK, Jochem, I adapted your example and got it working. Thank you very much.

I am still playing with it to better understand. One thing I don't yet understand is the necessity for the getFoo()/getBar() "handshake," especially the getbar() in the BAR class. That doesn't seem to serve any purpose. My adaptation us just a getDummy().

Do they just serve to pass the object by reference?


Ken

--------------------------------------------------------------
On Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:



class Foo
{
    private $var;
    function __construct() { $this->var = "foo"; }
    function getFoo() { return $this->var; }
}

class Bar
{
    private $var;
    private $foo;
    function __construct() { $this->var = "bar"; $this->foo = new Foo; }
    function getBar() { return $this->var; }
function speak() { echo "I am ",$this->foo->getFoo(),$this->getBar(),"\n"; }
}



--PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Assigning another class to a class variable is a standard OO technique, so it's not surprising that it's implemented in PHP. I believe it's called "composition", though that term seems to have a wider definition.

What is it that seems odd about what you call the getFoo()/getBar() "handshake" ? Actually, what do you mean by that? I could be missing something, but at the moment the two classes and their calls seem pretty straight-forward.

--

_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

--- End Message ---
--- Begin Message ---
Ken Kixmoeller -- reply to [EMAIL PROTECTED] wrote:
> OK, Jochem, I adapted your example and got it working. Thank you very much.
> 
> I am still playing with it to better understand. One thing I don't yet
> understand is the necessity for the getFoo()/getBar() "handshake,"
> especially the getbar() in the BAR class. That doesn't seem to serve any
> purpose. My adaptation us just a getDummy().
> 
> Do they just serve to pass the object by reference?

no - the whole piece of code was just a silly example to show you
that you can stuff an object into the property of another object.

objects are always by reference in php5 - in php4 you have to
use the 'reference' operator (the @ symbol) to make object be passed by 
reference.

your original question showed a new object being made in the constructor
of another object - that is fine but it seems a little pointless to
worry about referencing some 'global' [connection] object in each relevant
class when you seem to be creating a new object in each constructor.

try this example (I assume you use php5 - which I think you are because
you mentioned using the __construct() method which is a php5 only bit of
functionality):

<?php

class DBConn {
        private $userCnt = 0;
        function incrementUC() { $this->userCnt++; }
        function getUC() { return $this->userCnt;  }
}

class DOExample {
        private $dbconn;

        function __construct() {
                $this->dbconn = getDBConn();
                $this->dbconn->incrementUC();
        }
}

function getDBConn() {
        static $conn;

        if (!isset($conn))
                $conn = new DBConn();
        
        return $conn;
}

$a = new DOExample;
$c = new DOExample;
$b = new DOExample;

$d = getDBConn();
echo $d->getUC(),"\n";

?>

> 
> 
> Ken
> 
> --------------------------------------------------------------
> On Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:
> 
> 
>>
>> class Foo
>> {
>>     private $var;
>>     function __construct() { $this->var = "foo"; }
>>     function getFoo() { return $this->var; }
>> }
>>
>> class Bar
>> {
>>     private $var;
>>     private $foo;
>>     function __construct() { $this->var = "bar"; $this->foo = new Foo; }
>>     function getBar() { return $this->var; }
>>     function speak() { echo "I am
>> ",$this->foo->getFoo(),$this->getBar(),"\n"; }
>> }
>>
>>
> 
> --PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

--- End Message ---
--- Begin Message ---
I have date in the variable $date_reference in the format YYYY-MM-DD.
How do I find out the date before this and the date after this?  Ron

--- End Message ---
--- Begin Message ---
Read http://us3.php.net/manual/en/function.strtotime.php.

It might help.

On 1/27/07, Ron Piggott <[EMAIL PROTECTED]> wrote:
I have date in the variable $date_reference in the format YYYY-MM-DD.
How do I find out the date before this and the date after this?  Ron

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--- End Message ---
--- Begin Message ---
On sáb, 2007-01-27 at 23:51 -0500, Ron Piggott wrote:
> I have date in the variable $date_reference in the format YYYY-MM-DD.
> How do I find out the date before this and the date after this?  Ron
> 


http://us3.php.net/manual/en/function.date.php#68716

RTFM :-P

Attachment: signature.asc
Description: This is a digitally signed message part


--- End Message ---

Reply via email to