# Constant Parameters

- Thread: https://externals.io/message/28203
- Original archive: https://news-web.php.net/php.internals/28203
- Started by Jakob Buchgraber on 2007-03-02T18:25:50+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #28203 — Jakob Buchgraber — 2007-03-02T18:25:50+00:00

URL: https://externals.io/message/28203#28203

Hello!

It would be nice if in PHP 6 using constant parameters for functions and 
methods would be possible. This feature is e.g. available in Java.

function doSth (const A) {
     if (defined ("A")) echo "A is a constant";
}

doSth ("foo");

Cheers,
Jay

---

## #28204 — Brian Moon — 2007-03-02T19:22:05+00:00

URL: https://externals.io/message/28203#28204 · In reply to: #28203

Jakob Buchgraber wrote:
> Hello!
> 
> It would be nice if in PHP 6 using constant parameters for functions and 
> methods would be possible. This feature is e.g. available in Java.
> 
> function doSth (const A) {
>     if (defined ("A")) echo "A is a constant";
> }
> 
> doSth ("foo");
> 
> Cheers,
> Jay
> 

Hmm, can you give me a better example of how that would be used?  I 
don't see the use in this short example.

-- 

Brian Moon
-------------
http://dealnews.com/
It's good to be cheap =)

---

## #28205 — Marco Kaiser — 2007-03-02T19:44:32+00:00

URL: https://externals.io/message/28203#28205 · In reply to: #28203

Hi Jakob,
> It would be nice if in PHP 6 using constant parameters for functions
> and methods would be possible. This feature is e.g. available in Java.
>
> function doSth (const A) {
>     if (defined ("A")) echo "A is a constant";
> }
>
> doSth ("foo");
>
> Cheers,
> Jay
>
This makes absolute no sense, why you want this?

-- Marco

---

## #28206 — Jakob Buchgraber — 2007-03-02T20:24:17+00:00

URL: https://externals.io/message/28203#28206 · In reply to: #28205

Marco Kaiser wrote:
> Hi Jakob,
> > […]
> This makes absolute no sense, why you want this?
> 
> -- Marco

It's just a way of ensuring that a parameter cannot be changed within a 
method (e.g. an id). I am using this feature frequently in Java. It 
actually got no "deeper" sense, but it's just a nice feature I'd like to 
use in PHP too. :-)

Cheers,
Jay

---

## #28207 — Pierre — 2007-03-02T20:26:07+00:00

URL: https://externals.io/message/28203#28207 · In reply to: #28206

> Marco Kaiser wrote:
> > […]
>
> It's just a way of ensuring that a parameter cannot be changed within a
> method (e.g. an id). I am using this feature frequently in Java. It
> actually got no "deeper" sense, but it's just a nice feature I'd like to
> use in PHP too. :-)

It actually comes from the C/C++ "const" like foo(const char *str). It
makes very little sense to have it in PHP.

--Pierre

---

## #28209 — Guilherme Blanco — 2007-03-02T21:13:45+00:00

URL: https://externals.io/message/28203#28209 · In reply to: #28207

Hi,


I think it's a great possibility too, specially when dealing with 3rd
party components.
If you have an interface in your application to be implemented by
others, you can notice them about the constant argument (and should
NOT be redefined) and throw a fatal error (altho I still do not
understand why the notice, warning and fatal errors are not
throwable).

Notice that I am mentioning this since PHP5 treats object arguments as
references. So, a modification of a property in a const argument MUST
be impossible. If PHP keeps the arguments as by-value, I would
disagree with Jakob's suggestion; but, since you can modify a
referenced object, this feature should exist.

I am not able to imagine a functional example, but you can imagine a
situation of a Database argument being sent in a function:


$db = new DBConnection($properties);
// Notice, still not connected to DB
$third_party_obj->foo($db);
$db->connect();


Now, imagine $third_party_obj is an instance of 3rd-party implemented
feature... something like:


class MyCustomObj implements Extandable3rdParty {
//...
}


Ok... the idea is finished here... using a const argument, this should
not be possible:

public function foo($db) {
    $db->setDatabase("anotherDB");
    //....
}


Why? Because $db should be a constant and could not be modified.
Allowing this, the 3rd party component could redefine some important
properties inside of your object and may cause unbelievable issues in
your appplication.
My example illustrates a possible thing (a database name change)
without the use of constants. If this feature exists, a fatal error is
displayed pointing to the setDatabase line... a possible message: "You
cannot modify a constant argument [0]"


Maybe now you agree with his idea.



Best regards,


> 
> > […]
>
> It actually comes from the C/C++ "const" like foo(const char *str). It
> makes very little sense to have it in PHP.
>
> --Pierre
>
> --
> 
> 
>
>


-- 
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9166-6902
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Carlos - SP/Brazil

---

## #28210 — Sean Coates — 2007-03-02T21:25:44+00:00

URL: https://externals.io/message/28203#28210 · In reply to: #28209

> $db = new DBConnection($properties);
> // Notice, still not connected to DB
> $third_party_obj->foo($db);
> $db->connect();

In the requested feature, the "protection" is in the wrong hands (that
of the third party object).

You're much better off changing your call to:
$third_party_obj->foo(clone $db);

S

---

## #28211 — Brian Moon — 2007-03-02T21:26:38+00:00

URL: https://externals.io/message/28203#28211 · In reply to: #28209

Guilherme Blanco wrote:
> Why? Because $db should be a constant and could not be modified.
> Allowing this, the 3rd party component could redefine some important
> properties inside of your object and may cause unbelievable issues in
> your appplication.

This makes it sound like you let other people write code that you then 
include in your applications on your site.  Is that right?  Otherwise, 
don't you (or your team) write the code that goes on your site?

On top of that, this 3rd party will be making the function.  So, they 
control the definition, not you.  You can call time pass as const.  So, 
this is not a solution to keep 3rd parties from breaking your stuff.

It makes no sense to me, but you can never tell what OOP person may jump 
in here and think it is the best thing since left handed backwards 
inheritance or something. =)

-- 

Brian Moon
-------------
http://dealnews.com/
It's good to be cheap =)

---

## #28212 — Brian Moon — 2007-03-02T21:31:01+00:00

URL: https://externals.io/message/28203#28212 · In reply to: #28211

Brian Moon wrote:
> You can call time pass as const.

Damn, need to proof read.  Should be "CAN'T"

-- 

Brian Moon
-------------
http://dealnews.com/
It's good to be cheap =)
