php-general Digest 15 Oct 2006 08:46:15 -0000 Issue 4402
Topics (messages 243148 through 243158):
Regular expressions
243148 by: Morten Twellmann
243152 by: Penthexquadium
Re: A no brainer...
243149 by: Larry Garfield
243151 by: Ed Lazor
Re: Retrieving values from array on a class
243150 by: Kevin Waterson
243154 by: AR
243155 by: Larry Garfield
243157 by: AR
243158 by: Larry Garfield
Interchange.
243153 by: João Cândido de Souza Neto
Re: Class returning more than one value
243156 by: Sumeet
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 ---
I'm trying to understand these regular expressions, but I can't make them
work...
All I want to do, is to find the first occurrence of some text inside the
HTML tags <h1> and </h1>.
Example string: "<p>October 14, 2006</p><h1>Welcome to my
homepage</h1><p>We're happy to announce...</p>"
must return:
"Welcome to my homepage"
I tried with:
preg_match('<h1\b[^>]*>(.*?)</h1>', "<h1>Welcome to my homepage</h1>",
$matches, PREG_OFFSET_CAPTURE);
print_r($matches);
but got nothing...
Can anyone tell me how to do this?
(I tried the above expression in EditPad Pro 6 and it worked...!)
Sincerely,
Morten Twellmann
--- End Message ---
--- Begin Message ---
On Sat, 14 Oct 2006 23:19:13 +0200, "Morten Twellmann" <[EMAIL PROTECTED]>
wrote:
> I'm trying to understand these regular expressions, but I can't make them
> work...
>
> All I want to do, is to find the first occurrence of some text inside the
> HTML tags <h1> and </h1>.
>
> Example string: "<p>October 14, 2006</p><h1>Welcome to my
> homepage</h1><p>We're happy to announce...</p>"
>
> must return:
>
> "Welcome to my homepage"
>
>
> I tried with:
>
> preg_match('<h1\b[^>]*>(.*?)</h1>', "<h1>Welcome to my homepage</h1>",
> $matches, PREG_OFFSET_CAPTURE);
> print_r($matches);
>
> but got nothing...
>
> Can anyone tell me how to do this?
>
> (I tried the above expression in EditPad Pro 6 and it worked...!)
>
> Sincerely,
>
> Morten Twellmann
The regex you wrote lacks separator. That PHP statement will occur an
error:
> Warning: preg_match() [function.preg-match]: Unknown modifier ']' in
> /path/file.php on line X
The statement should be:
preg_match("/<h1\b[^>]*>(.*?)<\/h1>/i", "<h1>Welcome to my homepage</h1>",
$matches, PREG_OFFSET_CAPTURE);
print_r($matches);
--
Sorry for my poor English.
--- End Message ---
--- Begin Message ---
On Saturday 14 October 2006 11:02, Ed Lazor wrote:
> > Of course, the cost of serialization and deserialization is non-
> > trivial for
> > any data structure that is of interesting size, and you have to
> > keep in mind
> > that if you aren't syncing to the database periodically then you
> > will end up
> > with stale data objects. (An issue in any case, but the longer the
> > object
> > representing a view of your database exists, the more of a problem it
> > becomes. YMMV depending on the data you're holding.)
>
> Has anyone done tests on the difference between the value and
> performance of serializing data structures versus just pulling the
> data from the database?
>
> PHP stores session data in files by default. There's gotta be a
> performance hit for the file access. If you store session data in
> MySQL, you're still making a DB query. It seems to me that the
> performance is almost the same, which means grabbing the current
> record ends up better because you avoid stale data. What do you think?
>
> -Ed
It depends on what your data is.
Is your data basic (a few elements in a linear array) or complex (a deeply
nested multi-dimensional array or complex object?) Deserializing a complex
data structure can get expensive.
Is your data built by a single simple query against the database, a single but
very complex query with lots of joins and subqueries, or a bunch of separate
queries over the course of the program? A single SQL query for cached data
is likely faster than lots of little queries.
Is your data something that's going to change every few seconds, every few
minutes, or every few days? Caching something that will change by your next
page request anyway is a waste of cycles.
Is your data needed on every page load? Putting a complex data structure into
the session if you only need it occasionally is a waste of cycles. You're
better off rebuilding it each time or implementing your own caching mechanism
that only loads on demand.
There is no general answer here.
--
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 ---
On Oct 14, 2006, at 10:00 AM, Tony Di Croce wrote:
I think that the cost of de-serializing a session stored in files
should be significantly LESS than the cost of doing so through a
database, for the following reasons:
1) The db will need to parse querys. Not an issue for files.
2) The session ID will tell PHP what file to open in O(1).
3) The entire session needs to be de-serialized, not just some
portion of it. The database is optimized for returning subsets of
all the data.
Sorry Tony, I should have been more clear. I already know that
storing session data in MySQL is faster than storing it in files. I
know that goes against what you're saying, but there are some
examples if you Google "PHP MySQL session performance". One of the
more interesting examples is http://shiflett.org/articles/guru-speak-
jan2005. PHP session management defaults to files because it's more
portable and the performance difference doesn't matter for small
sites with few concurrent users. MySQL also provides better
scaleability and security for session data.
On Oct 14, 2006, at 2:51 PM, Larry Garfield wrote:
It depends on what your data is.
Is your data basic (a few elements in a linear array) or complex (a
deeply
nested multi-dimensional array or complex object?) Deserializing a
complex
data structure can get expensive.
Is your data built by a single simple query against the database, a
single but
very complex query with lots of joins and subqueries, or a bunch of
separate
queries over the course of the program? A single SQL query for
cached data
is likely faster than lots of little queries.
Is your data something that's going to change every few seconds,
every few
minutes, or every few days? Caching something that will change by
your next
page request anyway is a waste of cycles.
Is your data needed on every page load? Putting a complex data
structure into
the session if you only need it occasionally is a waste of cycles.
You're
better off rebuilding it each time or implementing your own caching
mechanism
that only loads on demand.
There is no general answer here.
Good points Larry. I have to look back, but I think we were
originally talking about basic user data. ie. the user logs into the
site and we store their login information and access rights in a
session. That seems like basic enough information that it's better
to just store the user id in session data and grab the rest of their
information from the db - not much of a difference in performance,
plus you end up avoiding stale data. Anyway, I like your distinction
between simple and complex objects.
-Ed
--- End Message ---
--- Begin Message ---
This one time, at band camp, Roman Neuhauser wrote:
BTW, here is my class:
class returnConfigParams
{
var $a;
var $b;
var $c;
var $d;
function getMySQLParams()
{
include($_SERVER['DOCUMENT_ROOT']."/properties.php");
$values = array(0 => $a, 1 => $b, 2 => $c, 3 => $d);
return($values);
}
}
Why not a static method?
Create a config singleton and you can have an instance of your class
available anywhere by simply using
config::getInstance()
class config{
/*** Declare instance ***/
private static $instance = NULL;
/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
private function __construct() {
/*** nothing to see, move along ***/
}
/**
*
* Return an array instance
*
* @access public
*
* @return array
*
*/
public static function getInstance() {
if (!self::$instance)
{
/*** set this to the correct path and add some error checking ***/
include($_SERVER['DOCUMENT_ROOT']."/properties.php");
self::$instance = array($a, $b, $C);
}
return self::$instance;
}
/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/
private function __clone(){
}
} /*** end of class ***/
Just a thought
Kevin
--- End Message ---
--- Begin Message ---
Hello,
I've rewritten my class as you told me:
----------------------------------------------------------
class returnConfigParams
{
private static $instance = NULL;
var $a;
var $b;
var $c;
var $d;
private function __construct() {
}
// function that get the database parameters from "properties.php"
public static function getInstance() {
if (!self::$instance)
{
/*** set this to the correct path and add some error checking ***/
include($_SERVER['DOCUMENT_ROOT']."/properties.php");
self::$instance = array($a, $b, $c, $d);
}
return self::$instance;
}
private function __clone(){
}
}
----------------------------------------------------------
and i'm calling it with
returnConfigParams::getInstance(); (probably wrongly)
The question is how to access the individual elements of the array.
Can someone help me please ?
Cheers,
AR
--- End Message ---
--- Begin Message ---
On Saturday 14 October 2006 19:09, AR wrote:
> Hello,
>
> I've rewritten my class as you told me:
>
> ----------------------------------------------------------
> class returnConfigParams
>
> {
>
> private static $instance = NULL;
>
> var $a;
> var $b;
> var $c;
> var $d;
>
> private function __construct() {
> }
>
> // function that get the database parameters from "properties.php"
>
> public static function getInstance() {
>
> if (!self::$instance)
> {
> /*** set this to the correct path and add some error checking ***/
> include($_SERVER['DOCUMENT_ROOT']."/properties.php");
> self::$instance = array($a, $b, $c, $d);
> }
> return self::$instance;
> }
>
> private function __clone(){
> }
>
> }
> ----------------------------------------------------------
>
> and i'm calling it with
>
> returnConfigParams::getInstance(); (probably wrongly)
>
> The question is how to access the individual elements of the array.
I think you're still misunderstanding the concepts involved.
If you have an array assigned to a variable, you access elements of it with
[].
$foo = array('a', 'b', 'c');
print $foo[0]; // gives 'a'
$bar = array('a' => 'hello', 'b' => 'world');
print $foo['b']; // gives 'world'
http://us3.php.net/manual/en/language.types.array.php
It doesn't matter if you got the array as a return from a function or method
or not.
However, what you're doing here is horribly bastardizing classes and
singletons. :-) Generally, a getInstance() is used to return an instance of
a CLASS, not an array.
If you just want to access the properties of an object, you use the object
dereference operator, ->.
$foo = new returnConfigParams();
print $foo->a; // prints whatever is the value of the property var $a
http://us3.php.net/manual/en/language.oop.php
Of course, if you're trying to get database parameter information, then all of
this is grossly over-engineered.
properties.php:
<?php
$dbsettings['a'] = 'foo';
$dbsettings['b'] = 'bar';
$dbsettings['c'] = 'baz';
index.php (or whatever):
require('properties.php');
And you now have an array $dbsettings, which has the values you need.
Much simpler, and much faster.
--
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 ---
Hi,
> If you have an array assigned to a variable, you access elements of it with
> [].
>
> $foo = array('a', 'b', 'c');
>
> print $foo[0]; // gives 'a'
>
> $bar = array('a' => 'hello', 'b' => 'world');
>
> print $foo['b']; // gives 'world'
I know that.
I had my class written as:
----------------------------------------------------------------
class returnConfigParams
{
var $a;
var $b;
var $c;
var $d;
// function that get the database parameters from "properties.php"
function getMySQLParams()
{
include($_SERVER['DOCUMENT_ROOT']."/properties.php");
$mysql_parameters = array(0 => $a, 1 => $b, 2 => $c, 3 => $d);
return($mysql_parameters);
}
}
?>
----------------------------------------------------------------
and i got the array values with:
$params_file = New returnConfigParams;
$params = $params_file->getMySQLParams();
values were $params[0], etc...
everything was fine.
then, someone suggested i might write it like this, so i can call it
with returnConfigParams::getMySQLParams();
----------------------------------------------------------------
class returnConfigParams
{
private static $instance = NULL;
private function __construct() {
}
// function that get the database parameters from "properties.php"
public static function getMySQLParams() {
if (!self::$instance)
{
/*** set this to the correct path and add some error checking ***/
include($_SERVER['DOCUMENT_ROOT']."/properties.php");
self::$instance = array($a, $b, $c, $d);
}
return self::$instance;
}
private function __clone(){
}
}
?>
----------------------------------------------------------------
This way i can't get the array elements.
I've tried
$params = returnConfigParams::getMySQLParams();
but no good.
And that's the story.
Cheers,
AR
--- End Message ---
--- Begin Message ---
On Sunday 15 October 2006 03:19, AR wrote:
> Hi,
>
> > If you have an array assigned to a variable, you access elements of it
> > with [].
> >
> > $foo = array('a', 'b', 'c');
> >
> > print $foo[0]; // gives 'a'
> >
> > $bar = array('a' => 'hello', 'b' => 'world');
> >
> > print $foo['b']; // gives 'world'
>
> I know that.
>
> I had my class written as:
> ----------------------------------------------------------------
> class returnConfigParams
>
> {
>
> var $a;
> var $b;
> var $c;
> var $d;
>
>
> // function that get the database parameters from "properties.php"
> function getMySQLParams()
>
> {
>
> include($_SERVER['DOCUMENT_ROOT']."/properties.php");
>
> $mysql_parameters = array(0 => $a, 1 => $b, 2 => $c, 3 => $d);
>
> return($mysql_parameters);
>
> }
>
> }
>
> ?>
> ----------------------------------------------------------------
>
> and i got the array values with:
> $params_file = New returnConfigParams;
> $params = $params_file->getMySQLParams();
> values were $params[0], etc...
> everything was fine.
>
>
> then, someone suggested i might write it like this, so i can call it
> with returnConfigParams::getMySQLParams();
>
> ----------------------------------------------------------------
> class returnConfigParams
>
> {
>
> private static $instance = NULL;
>
> private function __construct() {
> }
>
> // function that get the database parameters from "properties.php"
> public static function getMySQLParams() {
>
> if (!self::$instance)
> {
> /*** set this to the correct path and add some error checking ***/
> include($_SERVER['DOCUMENT_ROOT']."/properties.php");
> self::$instance = array($a, $b, $c, $d);
> }
> return self::$instance;
> }
>
> private function __clone(){
> }
>
> }
>
> ?>
> ----------------------------------------------------------------
>
> This way i can't get the array elements.
> I've tried
> $params = returnConfigParams::getMySQLParams();
> but no good.
>
> And that's the story.
>
> Cheers,
> AR
What exactly does properties.php do/contain?
You are aware that the $a, $b, $c, and $d you reference in both versions are
not the properties of the object, but local variables, right? If you want
the object properties, you need to use $this->a, $this->b, etc.
I still think both methods are highly over-engineered, however.
--
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 ---
Hello everyone.
Firstly my apology for the off topic.
I'm a PHP professional since 2000's.
In a quite near future i'm interested in have a professional interchange in
a foreign country and i'm here to ask you for any information about it.
Thanks a lot.
--- End Message ---
--- Begin Message ---
Paul Scott wrote:
On Sat, 2006-10-14 at 11:06 +0100, Deckard wrote:
How can i code a class with a function that returns more than one value ?
1. return array();
2. return stdClass;
--Paul
another option.. can be avoid, is to use global variables or
references...and modify them in the function
--
Thanking You
Sumeet Shroff
http://www.prateeksha.com
Web Designers and PHP / Mysql Ecommerce Development, Mumbai India
--- End Message ---