php-general Digest 3 Sep 2009 00:37:52 -0000 Issue 6319
Topics (messages 297603 through 297617):
Re: IRC and English (Bible)
297603 by: tedd
Re: IRC and English
297604 by: Ford, Mike
Re: Date +30 comparison
297605 by: Ford, Mike
297608 by: tedd
Re: CodeWorks 09
297606 by: Ben Dunlap
Re: windows 5.2.10 PHP not working with phpinfo
297607 by: Fred Silsbee
Re: Generic decorators and type hinting
297609 by: Ben Dunlap
297610 by: David Kurtz
297611 by: Christoph Boget
297615 by: Ben Dunlap
Re: OO and "Singleton" class
297612 by: Andrea Giammarchi
Array references - how to unset() ?
297613 by: Martin Zvarík
297614 by: Robert Cummings
297616 by: Martin Zvarík
297617 by: Robert Cummings
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:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
At 9:40 AM +0100 9/2/09, Peter Ford wrote:
The American standardisation
of English spelling did quite enough damage to the beautiful language of
Shakespeare (who couldn't even spell his own name consistently), without any
more neologisms creeping in.
Okay, Shakespeare... "2B || !2B" is an example of what we were talking about.
As to Shakespeare spelling his name consistently, I can't comment.
But there is a "story" where Shakespeare assisted in the translation
of the King James version of the Bible.
Shakespeare was born in 1564 and supposedly worked on the Bible when
he was 46 years old. The Bible was published one year later in 1611.
In King James version of the Bible one can go to the 46th Psalm and
count 46 words from the beginning and find the work "Shake".
Similarly, one can count 46 words forward from the end of the 46
Psalm and find the word "Spear".
For proof of the word count, here's a link (count for yourself):
http://bartelby.org/108/19/46.html
Was this Shakespeare's addition or a coincidence? It's probably a
coincidence for I find it highly unlikely that Shakespeare influenced
the writing of the Bible, but I find it interesting speculation.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Peter Ford [mailto:[email protected]]
> Sent: 02 September 2009 09:40
>
> Words that are two lengthy: "of", "an", "to", "it" (etc.)
> Words that are too lengthy: "antidisestablishmentarianism",
> "internationalisation" and that other one that begins with
> "flocci..." something
>
> Sorry tedd :)
Ooh! Ooh! Ooh! Please, sir, I know that one: it's
"floccinaucinihilipilification"!
(God knows why I ever learnt that -- probably some pointless schoolboy
challenge 40-odd years ago!! Is that the one that means "the act of valuing as
worthless", or am I confusing it with something else?
Back on topic, I must admit that a lot of txtspk sails by without me noticing
-- I suspect as a legacy of online chatting on DEC-10s back in the 70s, using
10cps teletypes, with a line length limit of 80 characters!
Cheers!
Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: [email protected]
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to
http://disclaimer.leedsmet.ac.uk/email.htm
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: tedd [mailto:[email protected]]
> Sent: 01 September 2009 21:52
>
> At 2:47 PM -0400 9/1/09, Andrew Ballard wrote:
> >On Tue, Sep 1, 2009 at 1:27 PM, tedd<[email protected]>
> wrote:
> >> First get the date to seconds, like so:
> >>
> >> $today_date = '8/26/2009';
> >>
> >> $next_date = strtotime($today_date) + (86400 * 30);
> >>
> >
> >No. Due to Daylight Saving Time, many time zones have two days each
> >year when the number of seconds in a day is not 86400.
> >
>
> Arrggg.
>
> But good to know.
And if you absolutely insist on doing it this way, make sure you start in the
middle of the day -- if your base time is 12:00 noon (which is what I always
use in this situation), the furthest it can go because of DST is 11:00 or
13:00, which won't screw you up if all you're interested in is the date. ;)
Cheers!
Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: [email protected]
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to
http://disclaimer.leedsmet.ac.uk/email.htm
--- End Message ---
--- Begin Message ---
At 4:06 PM +0100 9/2/09, Ford, Mike wrote:
> -----Original Message-----
From: tedd [mailto:[email protected]]
Sent: 01 September 2009 21:52
At 2:47 PM -0400 9/1/09, Andrew Ballard wrote:
>On Tue, Sep 1, 2009 at 1:27 PM, tedd<[email protected]>
wrote:
>> First get the date to seconds, like so:
>>
>> $today_date = '8/26/2009';
>>
>> $next_date = strtotime($today_date) + (86400 * 30);
>>
>
>No. Due to Daylight Saving Time, many time zones have two days each
>year when the number of seconds in a day is not 86400.
>
Arrggg.
But good to know.
And if you absolutely insist on doing it this way, make sure you
start in the middle of the day -- if your base time is 12:00 noon
(which is what I always use in this situation), the furthest it can
go because of DST is 11:00 or 13:00, which won't screw you up if all
you're interested in is the date. ;)
Cheers!
Mike
Another good thing to know.
Thanks,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
> What I would do for UK PHP events :-(
Something like this perhaps?
http://conference.phpnw.org.uk/phpnw09/
Ben
--- End Message ---
--- Begin Message ---
THANKS!
Here is my current situation (if you care to look at it (under the windows )
list
I tried 5.3 and found out it had no php_mssql.
Then I retreated to 5.2.10.
Trying to get back to where I had everything worked, I downloaded 5.2.6:
(1) I uninstalled cgi 1.5
(2) php.ini has:
extension_dir = "C:\PHP\ext"
; doc_root = "C:\inetpub\wwwroot" commented out
doc_root =
ALL extensions are uncommented
I only need:
extension=php_mssql.dll
; **You CAN safely turn this off for IIS, in fact, you MUST.**
cgi.force_redirect = 0
ntwdblib and php.ini moved to
C:\WINDOWS
C:\WINDOWS\system
C:\WINDOWS\system32
IIS: properties->home directory->configuration C:\PHP\php5isapi.dll
with GET,HOST,POST,DEBUG
I also tried:
php5ts.dll
phpnsapi.dll
only php5isapi.dll seemed to work!
added to XP Prof SP3 path: ;C:\PHP;C:\PHP\ext
also created (just in case) PHPRC environment variable (alongside path)
containing C:\PHP;C:\PHP\ext
I gave IUSR_LANDON all powers
________________________________________results
this works:http://207.254.225.224:8080/phpinfo.php
http://207.254.225.224:8080/new_black_scholes.php
initial display works until I select "DEMO"...then:
The page cannot be displayed
There is a problem with a program on the page you are trying to reach, and the
page cannot be displayed.
Here is new_black_scholes.php
<?php
define( "ITMAX",100);
define( "EPS",3.0e-7);
// If the submit button has been pressed
$StockPrice = $ExercisePrice = $RiskFreeRateInterest =
$InstantaneousVarianceRateStocksReturn = "";
$TimetoExpirationOption = $ValueCallOption = $ValuePutOption = $DeltaCalls
= $DeltaPuts = "";
if (isset($_POST['reset']))
{
$m_s = 100.;
$m_e = 100.;
$m_rf = .12;
$m_sigma = .1;
$m_time = 365.;
Black_Scholes_Main($m_s, $m_e, $m_rf, $m_sigma,$m_time, $m_c, $m_p,
$m_deltacalls, $m_deltaputs);
$StockPrice = $m_s;
$ExercisePrice = $m_e;
$RiskFreeRateInterest = $m_rf;
$InstantaneousVarianceRateStocksReturn = $m_sigma;
$TimetoExpirationOption = $m_time;
$ValueCallOption = $m_c;
$ValuePutOption = $m_p;
$DeltaCalls = $m_deltacalls;
$DeltaPuts = $m_deltaputs;
}
elseif (isset($_POST['submit']))
{
$m_s = $_POST['StockPrice'];
$m_e = $_POST['ExercisePrice'];
$m_rf = $_POST['RiskFreeRateInterest'];
$m_sigma = $_POST['InstantaneousVarianceRateStocksReturn'];
$m_time = $_POST['TimetoExpirationOption'];
Black_Scholes_Main($m_s, $m_e, $m_rf, $m_sigma,$m_time, $m_c, $m_p,
$m_deltacalls, $m_deltaputs);
$StockPrice = $m_s;
$ExercisePrice = $m_e;
$RiskFreeRateInterest = $m_rf;
$InstantaneousVarianceRateStocksReturn = $m_sigma;
$TimetoExpirationOption = $m_time;
$ValueCallOption = $m_c;
$ValuePutOption = $m_p;
$DeltaCalls = $m_deltacalls;
$DeltaPuts = $m_deltaputs;
}
function Black_Scholes_Main($m_s, $m_e, $m_rf, $m_sigma, $m_time, &$m_c, &$m_p,
&$m_deltacalls, &$m_deltaputs) {
$m_c = black_scholes($m_s, $m_e, $m_rf, $m_sigma, $m_time/365.,
$nd1, $nd2);
$m_p = $m_e / pow(1.+$m_rf, $m_time/365.) - $m_s + $m_c;
$m_deltacalls = $nd1;
$m_deltaputs = $nd1 - 1.;
}
function black_scholes( $s, $e, $rf, $sigma, $time, &$nd1, &$nd2) {
$num = log($s/$e)+$time*($rf+.5*$sigma*$sigma);
$d1 = $num/($sigma*sqrt($time));
$d2 = $d1 - $sigma*sqrt($time);
$c = $s*myerf($d1) - $e * myerf($d2) * exp(-$rf*$time);
$nd1 = myerf($d1);
$nd2 = myerf($d2);
return $c;
}
function gammln($xx)
{
$cof=array(76.18009173,-86.50532033,24.01409822,
-1.231739516,0.120858003e-2,-0.536382e-5);
$x=$xx-1.0;
$tmp=$x+5.5;
$tmp -= ($x+0.5)*log($tmp);
$ser=1.0;
for ($j=0;$j<=5;$j++) {
$x += 1.0;
$ser += $cof[$j]/$x;
}
return -$tmp+log(2.50662827465*$ser);
}
function gser( &$gamser, $a, $x, &$gln)
{
$gln=gammln($a);
if ($x <= 0.0) {
if ($x < 0.0) echo "x less than 0 in routine GSER";
$gamser=0.0;
return;
} else {
$ap=$a;
$sum=1.0/$a;
$del=$sum;
for ($n=1;$n<=ITMAX;$n++) {
$ap += 1.0;
$del *= $x/$ap;
$sum += $del;
if (abs($del) < abs($sum)*EPS) {
$gamser=$sum*exp(-$x+$a*log($x)-($gln));
return;
}
}
echo "a=$a too large, ITMAX = $itmax too small in routine GSER
";
return;
}
}
function gcf( &$gammcf,$a,$x,&$gln)
{
$gold=0.0;
$fac=1.0;
$b1=1.0;
$b0=0.0;
$a0=1.0;
$gln=gammln($a);
$a1=$x;
for ($n=1;$n<=ITMAX;$n++) {
$an=(double) $n;
$ana=$an-$a;
$a0=($a1+$a0*$ana)*$fac;
$b0=($b1+$b0*$ana)*$fac;
$anf=$an*$fac;
$a1=$x*$a0+$anf*$a1;
$b1=$x*$b0+$anf*$b1;
if ($a1) {
$fac=1.0/$a1;
$g=$b1*$fac;
if (abs(($g-$gold)/$g) < EPS) {
$gammcf=exp(-$x+$a*log($x)-($gln))*$g;
return;
}
$gold=$g;
}
}
echo "a too large, ITMAX too small in routine GCF
";
}
function gammp($a,$x)
{
if ($x < 0.0 || $a <= 0.0) {
echo "Invalid arguments in routine GAMMP
";
return 0.;
}
if ($x < ($a+1.0)) {
gser($gamser,$a,$x,$gln);
return $gamser;
} else {
gcf($gammcf,$a,$x,$gln);
return 1.0-$gammcf;
}
}
function gammq($a,$x)
{
if ($x < 0.0 || $a <= 0.0) echo "Invalid arguments in routine GAMMQ
";
if ($x < ($a+1.0)) {
gser($gamser,$a,$x,$gln);
return 1.0-$gamser;
} else {
gcf($gammcf,$a,$x,$gln);
return $gammcf;
}
}
function erfc($x)
{
return $x < 0.0 ? 1.0+gammp(0.5,$x*$x) : gammq(0.5,$x*$x);
}
function erf($x)
{
return $x < 0.0 ? -gammp(0.5,$x*$x) : gammp(0.5,$x*$x);
}
function myerf($argin) {
return .5*(1.+erf($argin/sqrt(2.0)));
}
?>
<form action="new_black_scholes.php" method="post">
<p>
Black Scholes Option Price Calculator:
temp website under Redhat Fedora 9 Linux:
the first 5 boxes require input(try 100. 100. .12 .1 365.):
</p>
<p>
StockPrice (required):
<input type="text" size="20" maxlength="40" name="StockPrice"
value="<?php echo $StockPrice; ?>" />
</p>
<p>
ExercisePrice (required):
<input type="text" size="20" maxlength="40" name="ExercisePrice"
value="<?php echo $ExercisePrice; ?>" />
</p>
<p>
Risk Free Rate of Interest(required):
<input type="text" size="20" maxlength="40" name="RiskFreeRateInterest"
value="<?php echo $RiskFreeRateInterest; ?>" />
</p>
<p>
Instantaneous Variance Rate of Stock's Return (required):
<input type="text" size="20" maxlength="40"
name="InstantaneousVarianceRateStocksReturn"
value="<?php echo $InstantaneousVarianceRateStocksReturn; ?>" />
</p>
<p>
Time to Expiration of the Option(days) (required):
<input type="text" size="20" maxlength="40"
name="TimetoExpirationOption"
value="<?php echo $TimetoExpirationOption; ?>" />
</p>
<p>
Values of the Call Option :
<input type="text" size="20" maxlength="40" name="ValueCallOption"
VALUE="<?php echo $ValueCallOption; ?>" />
</p>
</p>
<p>
Values of the Put option :
<input type="text" size="20" maxlength="40" name="ValuePutOption"
VALUE="<?php echo $ValuePutOption; ?>" />
</p>
<p>
Delta(calls):
<input type="text" size="20" maxlength="40" name="DeltaCalls"
VALUE="<?php echo $DeltaCalls; ?>" />
</p>
<p>
Delta(puts):
<input type="text" size="20" maxlength="40" name="DeltaPuts"
VALUE="<?php echo $DeltaPuts; ?>" />
</p>
<button type="submit" name = "submit" value="Calculate!"
style="color:maroon font:18pt Courier; font-weight:bold ">Calculate
</button>
<button type="submit" name = "reset" value="Demo!"
style="color:red font:18pt Courier; font-weight:bold ">Demo
</button>
</form>
_________________________________________________________results
Please try the following:
Open the 207.254.225.224:8080 home page, and then look for links to the
information you want.
Click the Refresh button, or try again later.
HTTP 403.1 Forbidden: Execute Access Forbidden
Internet Information Services
--------------------------------------------------------------------------------
Technical Information (for support personnel)
Background:
You have attempted to execute a CGI, ISAPI, or other executable program from a
directory that does not allow programs to be executed.
More information:
Microsoft Support
____________________________________________
http://207.254.225.224:8080/handle_log_book_mssql.php
yields:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server:
LANDON\SQLEXPRESSLMKIII in C:\Inetpub\wwwroot\handle_log_book_mssql.php on line
8 (mssql_connect(...)
Something went wrong while connecting to MSSQL (my message)
_______________________________________________________
all these worked before trying 5.3
--- On Wed, 9/2/09, hack988 hack988 <[email protected]> wrote:
> From: hack988 hack988 <[email protected]>
> Subject: Re: [PHP] windows 5.2.10 PHP not working with phpinfo
> To: "Fred Silsbee" <[email protected]>
> Cc: [email protected]
> Date: Wednesday, September 2, 2009, 6:03 AM
> Use Event Viewer and log type is
> :Application log
>
> 2009/9/2 Fred Silsbee <[email protected]>:
> > I looked high and low but no syslog
> >
> > I am currently working under XP Prof SP3
> >
> > --- On Mon, 8/31/09, hack988 hack988 <[email protected]>
> wrote:
> >
> >> From: hack988 hack988 <[email protected]>
> >> Subject: Re: [PHP] windows 5.2.10 PHP not working
> with phpinfo
> >> To: "Fred Silsbee" <[email protected]>
> >> Cc: [email protected]
> >> Date: Monday, August 31, 2009, 6:35 AM
> >> Please set
> >>
> log_error=on,error_reporting=E_ALL,error_log=syslog in
> >> php.ini and then,see error detail in syslog.
> >>
> >> 2009/8/31 Fred Silsbee <[email protected]>:
> >> > I got 5.3 working but found out there was no
> >> php_mssql.dll for it.
> >> > Somebody (who didn;t know) said I had to
> return to
> >> 5.2.8 but I found no 5.2.8 so I am trying 5.2.10
> >> >
> _________________________________________problem:
> >> > under IE8:
> >> > http://72.47.28.128:8080/phpinfo.php
> >> > with:
> >> > <?php
> >> > phpinfo();
> >> > ?>
> >> >
> >> > I get :
> >> > The website cannot display the page
> >> > HTTP 500
> >> > Most likely causes:
> >> > •The website is under maintenance.
> >> > •The website has a programming error.
> >> > ___________________________________________
> >> >
> >> > I installed :
> >> > php-5.2.10-Win32-VC6-x86.zip and put php.ini
> in C:\PHP
> >> and C:\PHP\ext
> >> > AND C:\WINDOWS, C:\WINDOWS\system and
> >> C:\WINDOWS\system32
> >> >
> >> > I installed FastCGI 1.5 !
> >> >
> >> > In php.ini I put :
> >> > ________________________________
> >> >
> >> > cgi.force_redirect = 0
> //
> >> for CGI
> >> >
> >> > extension_dir = "C:\PHP\ext"
> >> >
> >> > commented out
> >> > ;doc_root = "C:\inetpub\wwwroot" // for
> IIS/PWS
> >> > leaving
> >> > doc_root =
> >> >
> _________________________________________________
> >> > IIS 5.1 properties->configuration I added
> .php
> >> C:\PHP\php5ts.dll
> >> > GET,HEAD,POST,DEBUG
> >> >
> >> > Maybe php-win.exe
> >> >
> _________________________________________________
> >> >
> >> > I added to the XP Prof environment path
> >> ;C:\PHP\;C:\PHP\ext\
> >> >
> >> > I created an environment variable (and
> rebooted) PHPRC
> >> = C:\PHP;C:\PHP\ext
> >> >
> >> >
> >> > I never found any statement of the necessity
> of
> >> requiring CGI
> >> >
> >> > The instructions ramble around
> >> >
> >> >
> >> >
> >> >
> >> >
> >> >
> >> > --
> >> > PHP General Mailing List (http://www.php.net/)
> >> > To unsubscribe, visit: http://www.php.net/unsub.php
> >> >
> >> >
> >>
> >
> >
> >
> >
>
--- End Message ---
--- Begin Message ---
> Is there another way to cleanly wrap method calls for timing/logging
> purposes?
I have a possibly-evil idea that gets around type-hinting by
dynamically declaring decorator classes as children of the real
classes that need to be timed. You end up with as many "decorators" as
you have classes that need to be timed, but if this is for dev/QA
purposes only, that might not be a problem.
This method can't have the conceptual simplicity of yours,
unfortunately, because __call() will never be invoked in a child
class. The original parent's methods will just be called instead. But
you can still override the parent's methods with wrapper methods in
the child class.
I've got some sample code written that first calls get_class_methods()
to list the names of all the visible methods of the parent object,
then constructs code to declare a child class that overrides those
methods with wrapper methods, and finally eval()s the declaration and
instantiates an object of the new class.
The basic concept seems to work and to get along fine with
type-hinting. I'm happy to share the test code if you're interested.
Not sure how it would end up working in real life, and I'm guessing
there are more sophisticated ways to achieve the same concept, perhaps
using the Reflection API. I suspect it also needs some refining to
handle protected methods. But it might be a start.
Ben
--- End Message ---
--- Begin Message ---
On Sep 2, 2009, at 12:17 PM, Ben Dunlap wrote:
I've got some sample code written that first calls get_class_methods()
to list the names of all the visible methods of the parent object,
then constructs code to declare a child class that overrides those
methods with wrapper methods, and finally eval()s the declaration and
instantiates an object of the new class.
You're right; that is evil.
But, interesting idea. It turns out for my specific purposes (a
generic decorator that measures a class's method execution times), a
full-fledged profiler like APD is really what I need, but I'll keep
this in mind for getting around the type-hinting, should the need
arise again.
Thanks.
--
David Kurtz
[email protected]
--- End Message ---
--- Begin Message ---
> I have a possibly-evil idea that gets around type-hinting by
> dynamically declaring decorator classes as children of the real
> classes that need to be timed. You end up with as many "decorators" as
> you have classes that need to be timed, but if this is for dev/QA
> purposes only, that might not be a problem.
You don't even need to do that; it'd generate way too much redundant
code. Instead, just use interfaces. The only real downside is that
all the classes you want to decorate would need to implement them and
that would cause a wee bit of ugliness in the code/class declaration.
But if you are fine with that, it's a perfect solution, particularly
since you can type hint interfaces.
interface iDecorator {}
class Thing implements iDecorator {}
class Timer
{
public static function time( iDecorator $obj ) {}
}
$obj = new Thing();
Timer::time( $obj ); // $obj becomes a Timer obj with $obj inside it
$obj->thingMethod(); // Timer passes call to Timer->obj
thnx,
Christoph
--- End Message ---
--- Begin Message ---
> code. Instead, just use interfaces. The only real downside is that
> all the classes you want to decorate would need to implement them and
> that would cause a wee bit of ugliness in the code/class declaration.
Can you explain a bit more? As I understood the OP, the challenge was
to take a large, already-built code base that relies on Zend Framework
(which itself has 1600 classes), and wrap arbitrary existing methods
with timing logic -- without significant code changes.
As I understand your solution, it would require all pre-existing
classes to be modified to implement the iDecorator interface -- and
even then, pre-existing methods in those pre-existing classes would
not actually be affected. So those would have to be modified also.
But maybe I'm totally missing something?
Ben
--- End Message ---
--- Begin Message ---
Maybe somebody would be interested in my solution as well:
http://webreflection.blogspot.com/2009/09/php-53-singleton-fast-and-abstract.html
Regards
> Date: Wed, 2 Sep 2009 14:02:25 +0900
> From: [email protected]
> To: [email protected]
> Subject: Re: [PHP] OO and "Singleton" class
>
> Hi,
>
> After PHP 5.3, there is better implements for Singleton.
>
> http://d.hatena.ne.jp/Yudoufu/20090811/1250021010
>
> This entry was written by Japanese, but you may understand php code. :-)
>
> On Wed, Sep 2, 2009 at 07:06, Eddie Drapkin<[email protected]> wrote:
> > On Tue, Sep 1, 2009 at 5:16 PM, Paul M Foster<[email protected]>
> > wrote:
> >>
> >> I'm a little fuzzy on some of the PHP implementation details for some
> >> stuff. In PHP (5 <= phpversion() < 5.3), I'd like a configuration class
> >> which can only effectively be instantiated once. Will the following code
> >> do this? Any other suggestions or corrections? (Code is untested. Feel
> >> free to complain about syntax.)
> >>
> >> class config
> >> {
> >> private static $cfg;
> >>
> >> // Initializes the configuration from main config file
> >> function __construct()
> >> {
> >> if (!is_array(self::$cfg)) {
> >> if (defined('CFGFILE')) {
> >> include(CFGFILE);
> >> self::$cfg = $config;
> >> }
> >> else
> >> self::$cfg = array();
> >> }
> >> }
> >>
> >> // Returns a config item or null if not found
> >> function item($index)
> >> {
> >> if (array_key_exists($index, self::$cfg))
> >> return self::$cfg[$index];
> >> else
> >> return null;
> >> }
> >>
> >> // Sets a config item, optionally overwriting.
> >> // Returns true on success, false on failure.
> >> function set($index, $value, $overwrite = true)
> >> {
> >> $write = false;
> >> if (array_key_exists($index, self::$cfg) and $overwrite)
> >> $write = true;
> >> elseif (!array_key_exists($index, self::$cfg))
> >> $write = true;
> >> if ($write) {
> >> self::$cfg[$index] = $value;
> >> return true;
> >> }
> >> else
> >> return false;
> >> }
> >>
> >> };
> >>
> >> Paul
> >>
> >> --
> >> Paul M. Foster
> >>
> >> --
> >> PHP General Mailing List (http://www.php.net/)
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >>
> >
> > The primary thing you need to do to make a class a singleton is
> > disallow the instantiation of a class directly. Which means using
> > visibility, which is conspicuously absent from your example above.
> >
> > So, to prevent normal instantiation, we make the constructor private.
> > Also, we need a method to retrieve the instance. Static members serve
> > this purpose:
> >
> > class Singleton {
> > private static $instance;
> >
> > private function __construct() { }
> >
> > public static function getInstance() {
> > if( ! self::$instance instanceof Singleton) //replace Singleton
> > with class name
> > self::$instance = new Singleton; //replace Singleton with class name
> >
> > return self::$instance;
> > }
> >
> > public function __clone() {
> > throw new Exception('Cannot clone a Singleton');
> > }
> > }
> >
> > This class "shell" will prevent people from instantiating it ($foo =
> > new Singleton is an error). Instead, deployment will look like `$foo
> > = Singleton::getInstance()` which, as you can tell, always returns the
> > same instance. Throwing an exception out of __clone() will prevent
> > something like `$foo = Singleton::getInstance(); $bar = clone $foo;`
> > which is something I see overlooked a lot.
> >
> > So, if you wanted to convert your full class to a singleton it would
> > look something like:
> >
> > class config {
> > private static $instance;
> > private static $cfg;
> >
> > private function __construct() {
> > if(!is_array(self::$cfg)) {
> > if(defined('CFGFILE')) {
> > include(CFGFILE);
> > self::$cfg = $config;
> > } else {
> > self::$cfg = array();
> > }
> > }
> > }
> >
> > public function getInstance() {
> > if(!(self::$instance instanceof config)) {
> > self::$instance = new config();
> > }
> >
> > return self::$instance;
> > }
> >
> > public function __clone() {
> > throw new Exception("Cannot clone a Singleton.");
> > }
> >
> > public function item($index) {
> > if(array_key_exists($index, self::$cfg))
> > return self::$cfg[$index];
> > else
> > return null;
> > }
> >
> > // Sets a config item, optionally overwriting.
> > // Returns true on success, false on failure.
> > function set($index, $value, $overwrite = true) {
> > $write = false;
> > if (array_key_exists($index, self::$cfg) and $overwrite) {
> > $write = true;
> > } elseif (!array_key_exists($index, self::$cfg)) {
> > $write = true;
> > }
> >
> > if ($write) {
> > self::$cfg[$index] = $value;
> > return true;
> > } else {
> > return false;
> > }
> > }
> >
> >
> > }
> >
> > Note that this is also completely untested or anything of the sort.
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
>
> --
> 小山哲志@テックスタイル
> [email protected] : http://techstyle.jp/
> [email protected] : http://d.hatena.ne.jp/koyhoge/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
_________________________________________________________________
Share your memories online with anyone you want.
http://www.microsoft.com/middleeast/windows/windowslive/products/photos-share.aspx?tab=1
--- End Message ---
--- Begin Message ---
$ARR = array(
'a' => array('b' => 'blah')
);
function set($key)
{
global $ARR;
foreach ($key as $i => $k) {
if ($i == 0) {
$sourcevar =& $ARR[$k];
} else {
$sourcevar =& $sourcevar[$k];
}
}
// unset($sourcevar); // will cancel the reference - we want to
unset the ['b'], but how?
$sourcevar = null; // will set it NULL, but won't unset...
foreach ($ARR ... // I could run a cleanup, that would go through all of
the array and unset what is NULL, but I would need to use REFERENCES
again!! array_walk_recursive() is also worthless... any ideas?
}
set( array('a', 'b') );
--- End Message ---
--- Begin Message ---
Martin Zvarík wrote:
$ARR = array(
'a' => array('b' => 'blah')
);
function set($key)
{
global $ARR;
foreach ($key as $i => $k) {
if ($i == 0) {
$sourcevar =& $ARR[$k];
} else {
$sourcevar =& $sourcevar[$k];
}
}
// unset($sourcevar); // will cancel the reference - we want to
unset the ['b'], but how?
$sourcevar = null; // will set it NULL, but won't unset...
foreach ($ARR ... // I could run a cleanup, that would go through all of
the array and unset what is NULL, but I would need to use REFERENCES
again!! array_walk_recursive() is also worthless... any ideas?
}
set( array('a', 'b') );
unset( $ARR[$k] )
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
Robert Cummings napsal(a):
Martin Zvarík wrote:
$ARR = array(
'a' => array('b' => 'blah')
);
function set($key)
{
global $ARR;
foreach ($key as $i => $k) {
if ($i == 0) {
$sourcevar =& $ARR[$k];
} else {
$sourcevar =& $sourcevar[$k];
}
}
// unset($sourcevar); // will cancel the reference - we want to
unset the ['b'], but how?
$sourcevar = null; // will set it NULL, but won't unset...
foreach ($ARR ... // I could run a cleanup, that would go through all
of the array and unset what is NULL, but I would need to use
REFERENCES again!! array_walk_recursive() is also worthless... any
ideas?
}
set( array('a', 'b') );
unset( $ARR[$k] )
Cheers,
Rob.
Thanks for reply, but I want to:
unset($ARR['a']['b'])
Imagine I have this:
$KEYS = array('a', 'b', 'c');
And I want to:
unset($ARR['a']['b']['c'])
It's probably impossible, unless I do something dirty like this:
list($rootA, $rootB, $rootC) = $KEYS;
if (isset($rootC)) unset($x[$rootA][$rootB][$rootC]);
elseif (isset($rootB)) unset($x[$rootA][$rootB]);
elseif (isset($rootA)) unset($x[$rootA]);
--- End Message ---
--- Begin Message ---
Martin Zvarík wrote:
Robert Cummings napsal(a):
Martin Zvarík wrote:
$ARR = array(
'a' => array('b' => 'blah')
);
function set($key)
{
global $ARR;
foreach ($key as $i => $k) {
if ($i == 0) {
$sourcevar =& $ARR[$k];
} else {
$sourcevar =& $sourcevar[$k];
}
}
// unset($sourcevar); // will cancel the reference - we want to
unset the ['b'], but how?
$sourcevar = null; // will set it NULL, but won't unset...
foreach ($ARR ... // I could run a cleanup, that would go through all
of the array and unset what is NULL, but I would need to use
REFERENCES again!! array_walk_recursive() is also worthless... any
ideas?
}
set( array('a', 'b') );
unset( $ARR[$k] )
Cheers,
Rob.
Thanks for reply, but I want to:
unset($ARR['a']['b'])
Imagine I have this:
$KEYS = array('a', 'b', 'c');
And I want to:
unset($ARR['a']['b']['c'])
This is possible. You're just not giving enough consideration to your
exit strategy :)
<?php
function unset_deep( &$array, $keys )
{
$final = array_pop( $keys );
foreach( $keys as $key )
{
$array = &$array[$key];
}
unset( $array[$final] );
}
$value = array
(
'a' => array
(
'b' => array
(
'c' => 'C',
'd' => 'D',
'e' => 'E'
),
),
);
$keys = array('a', 'b', 'c');
unset_deep( $value, $keys );
print_r( $value );
?>
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---