php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39340 "With" keyword
Submitted: 2006-11-02 02:34 UTC Modified: 2010-12-22 13:10 UTC
Votes:3
Avg. Score:3.7 ± 1.9
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: thehub at lofty dot net dot au Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.4.4 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: thehub at lofty dot net dot au
New email:
PHP Version: OS:

 

 [2006-11-02 02:34 UTC] thehub at lofty dot net dot au
Description:
------------
Visual Basic has a neat little code trick that I'd like to see in PHP...
With object1
    .property1="value"
    .function1()
End With
And it can be nested. This is useful if you don't want to create a variable to hold the return of, e.g. a function call that returns an array, when you only need it for two or three lines.

Reproduce code:
---------------
with($nested_array['a_very_long_key']){
    $[0]='apples';
    with(function1($[1]->data))
        echo template($['title'],$['content']);
}

// or

with $object1:
    with $->property1:
        function1($['key_of_property1']);
    endwith
endwith


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-08 19:12 UTC] thehub at lofty dot net dot au
or perhaps for a long piece of code that would be used a lot. this ugly rectangle of code for example:

$server[$currServ][$currDB][$currGroup][$currUser]['access']=4;
$server[$currServ][$currDB][$currGroup][$currUser]['email']='';
$server[$currServ][$currDB][$currGroup][$currUser]['lastlogin']=0;
$server[$currServ][$currDB][$currGroup][$currUser]['projects']=array();

becomes

with($server[$currServ][$currDB][$currGroup][$currUser]){
    $['access']=4;
    $['email']='';
    $['lastlogin']=0;
    $['projects']=array()
}

kind of like a function that's created on the fly and then destroyed when it's finished
 [2008-04-09 07:38 UTC] ois at oisnot dot com
<pre><?php

$currServ = 'test1';
$currDB = 'test2';
$currGroup = 'test3';
$currUser = 'test4';

$server = array();
$server[$currServ][$currDB][$currGroup][$currUser]['access']=4;
$server[$currServ][$currDB][$currGroup][$currUser]['email']='';
$server[$currServ][$currDB][$currGroup][$currUser]['lastlogin']=0;
$server[$currServ][$currDB][$currGroup][$currUser]['projects']=array();
var_export($server);
unset($server);

$server = array();
$server[$currServ][$currDB][$currGroup][$currUser] =array();
$server_curr =& $server[$currServ][$currDB][$currGroup][$currUser];
$server_curr['access']=4;
$server_curr['email'] ='';
$server_curr['lastlogin']=0;
$server_curr['projects']=array();
var_export($server);
unset($server);
unset($server_curr);

$server = array();
$server_curr = array();
$server_curr['access']=4;
$server_curr['email'] ='';
$server_curr['lastlogin']=0;
$server_curr['projects']=array();
$server[$currServ][$currDB][$currGroup][$currUser] = $server_curr;
var_export($server);
?></pre>

Anyway, use objects.
 [2009-08-27 23:13 UTC] robeddielee at hotmail dot com
One interesting use for this would be as an alternative to the convention of using method chaining with setters. For example:

$definition->addField(new StringField('username'))
    ->setRequired(true)
    ->setReadonly(true);

Is becoming a popular convention, but requires all your setters to return $this, which I personally dislike, and this pattern cannot be used with existing APIs such as PEAR modules.

Using the with statement, would facilitate the method chaining setters pattern without requiring hacks to your APIs. For example:

with ($definition->addField(new StringField('username')))
{
    ->setRequired(true);
    ->setReadonly(true);
}

This would require only that addField return the added object, which is more palatable to me compared to all setters returning $this.
 [2010-12-22 13:10 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:10 UTC] johannes@php.net
Adding such a thing was discuessed multiple times and always rejected. Code like

$server[$currServ][$currDB][$currGroup][$currUser]['access']=4;
$server[$currServ][$currDB][$currGroup][$currUser]['email']='';
$server[$currServ][$currDB][$currGroup][$currUser]['lastlogin']=0;
$server[$currServ][$currDB][$currGroup][$currUser]['projects']=array();

can be written as


$t = &$server[$currServ][$currDB][$currGroup][$currUser];
$t['access']=4;
$t['email']='';
$t['lastlogin']=0;
$t['projects']=array()


without the need for a new construct.
 [2013-01-01 22:19 UTC] andries dot malan at gmail dot com
Another alternative to the WITH keyword, can be achieved by designing your API 
with arrays in mind, 
such as the following:

(Taking the example above with definitions having fields added with addField)

I believe designing your API's like this makes this much clearer especially if 
you use the latest php 
array syntax [ ] instead of array()

For example:

instead of writing code like

$definition->addField(new StringField('username'))
    ->setRequired(true)
    ->setReadonly(true);

and what if you wanted to add multiple fields? such as

$definition
  ->addField(new StringField('username'))
      ->setRequired(true)
      ->setReadonly(true)
   ->addField(new NumberField('age'))
      ->setWholeNumbersOnly(true)
      

To allow this would cause really mangled API code!


The WITH keyword would solve this by allowing nested WITH constructs.


But an array-based API would be cleaner in my opinion:


definition->addFields(
 [
   'username' => new StringField(
     [
       'required'=> TRUE,
       'readOnly'=> TRUE,
     ]),

   'age'  => new NumberField(
     [
        'wholeNumbersOnly'=>TRUE
     ]),

   'password => new PasswordField(),

   'balance' => new CurrencyField(),

   'birthDate' => new DateField()
]);


(Where attributes or lists are optional the API implements them with default 
function argument values)

This is how I implement my own API's.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 16:01:27 2024 UTC