Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding question, using WITH block type coding in PHP

Status
Not open for further replies.

weedz

Programmer
Dec 5, 2000
718
NL
Is it possible to use a VB/VFP etc WITH block type coding in PHP ?

Like for example:
Code:
With $object
   ->property = value;
   ->property2 = value2;
   ->dosomething();
End with

TIA.
 
nope.

but if you're really lazy you could do something like this

Code:
class foo{
 function bar(){
   return "hello world";
 }
}

$mylongobjectname = new foo;

//here comes the lazy bit
$a =& $mylongobjectname;
echo $a->bar();

inside a class then the special thingies of $this self:: and parent:: exist to make things easier.
 
Thanks. It is not laziness, but I find it more readable.

My code now looks like this:
Code:
$objRecordSet->selectionclause  = BuildSelectionClause();
$objRecordSet->joinclause       = BuildJoinClause();
$objRecordSet->whereclause      = SetUpWhereClause();
$objRecordSet->orderbyclause    = SetUpOrderClause();
$objRecordSet->limitclause      = SetupLimitClause();
$objRecordSet->Requery();

It would look nicer/more readable with a WITH block, oh well, too bad :(

 
I typically would do all that inside the a class method. something like this
Code:
<?php
/* example usage
 * $obj = new query ($query);
 * $obj->build();
 * $obj_reQuery();
 * 
 */
class query{
	
	public function __construct($query){
		$this->originalQuery = $query;
	}
	
	public function build(){
		$this->buildSelectionsClause();
		$this->buildJoinClause();
		$this->setUpWhereClause();
		$this->setUpOrderClause();
		$this->setUpLimitClause();
	}
	
	public function reQuery(){
		$this->assembleQuery();
		//do something with $this->query
	}
	private function assembleQuery(){
		$this->query = $this->selectClause . $this->joinClause . $this->whereClause . $this->orderClause. $this->limitClause; 
	}
	private function buildSelectionClause(){
		//do stuff
		$this->selectClause = $return;
	}
	private function buildJoinClause(){
		//do stuff
		$this->joinClause = $return;
	}
	private function setUpWhereClause(){
		//do stuff
		$this->whereClause = $return;
	}
	private function setUpOrderClause(){
		//do stuff
		$this->orderClause = $return;
	}
	private function setUpLimitClause(){
		//do stuff
		$this->limitClause = $return;
	}
}
?>
you could always make this even shorter by putting all the internal function calls inside the constructor.
 
Alas there is no such thing as a with block in PHP.

And no, it is not laziness, but a gentle nudge into neat programming. "With blocks" gently push you to group statements on one instance together and show that they form a group.

Once you realize that, a method is easily extracted and if the instance is of another class, it makes you clear that you can consider moving that code into another class.

It is a matter of style that can tell you something...

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
apologies if I was taken to infer that using a with block is lazy. I understand that within the MS architecture there is actually a performance enhancement as well as a style benefit in using with blocks, as the interpreter does not need to 'refind' the instance of whatever object is being addresses.

I was inferring, however, that creating a second variable in php, that would hold a reference to the first variable was laziness in that it involved less repetitive typing.

It is an unfortunate truth of coding that repetitious typing is rife and necessary. my fingers now remember the spatial movements for php functions as easily as they type my own name! we all develop coping mechanisms: such as reusable classes and libraries. my own nirvana has been to create a proper form builder and abstraction engine that will handle the creation, management, display and parsing of form data as well as the CRUD aspects of the data behind it. i've started this project night on 6 times and never finished it because i could never quite pin down the specification... the curse of programming for yourself!
 
Thank you all for the quick response, much appreciated !

@jpadie

I already do a lot inside my recordset class (that in its turn is based on my record class) for handling datasets.
The build clauses are actually depending on external variables such as posted values in a page, that is why I do it like this and not inside the class.

And I have the same problems as you do.
I started with php 4 years ago (just as a hobby, professionally I program in an MS environment (VFP, VB, .NET, the works).

Didn't know about OOP in PHP, and a year later saw most of the possibilities. So I recoded my website and made most of it OOP.
I am now in the phase of cleaning up most of my code, make it more readable and turn still existing procedural stuff in OOP wherever prudent - therefor my initial question.

Now I see in PHP 5 there are even more neat new features...will it ever stop...probably not :D




But thanks for the example anyways and thank you all for your time.

Weedz (Edward W.F. Veld)
 
OOP in php4 was very limited. php5 is/was a massive step closer to proper OOP.

i have not yet played around with php6 (which has been around in beta for a while now). I know that there are now namespaces implemented (which will make coding wordpress plugins _so_ much easier: no more static calls to public methods that really ought to be private. i suspect that classes have been improved too.

does your query handler actually parse incoming queries into their constituent parts? (like a lexer) If so i'd be very interested in taking a look. I have looked at a few sql parsers for a project I have (pdo_for_wordpress) and found them all to be lacking in quite serious areas. I ended up coding my solution using on-the-fly rewriting techniques, which is 'hacky' to the say the least.
 
Sorry my record class is a simple one record only handler.
My recordset class is just to show lists, walk through records, build dropdown lists etc. and doesn't do anything spectacular, but it serves its purpose. I just like to encapsulate data handling and not have SQL statements in my page scripts, for that reason I use these classes.


My hosting service has only just updated to PHP 5, so I am lagging behind on that part. I do have PHP 5 installed on my PC of course, but still have to learn a bit.

Namespaces would be cool indeed. Anyways, let's close this thread before it becomes a blog.

Thanks again. [thumbsup2]



Weedz (Edward W.F. Veld)
 
for future readers, php already includes a couple of classes that handle queries and recordsets natively. these are PDO, dba, dbx and ODBC. Of these I prefer PDO (which in turn provides an interface to ODBC if required). PDO can be easily extended by inherited classes.

Additionally there are many good quality php abstraction layers such as PEAR::MDB2 and adodb. These will perform a degree of sql rewriting and are well supported and documented.

If you are looking for portability in an app, take a good look at PDO - it's fast and very portable. But do not write your native code for mysql - it is far from ansi sql compliant. If you must design primarily for mysql, my advice would be to keep your queries very simple, do not use any mysql functions - instead code for simple selects, joins and sub-selects and rely on php to perform, for example, date manips etc. this is contrary to the advice I would give if you were coding definitively ONLY for mysql: in which case i'd say use those built-in functions to your heart's content!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top