I know I can force a variable passed to a function to be an object of
class bar
eg
function foo(bar $bar_object)
{
//code
}
but can I force a variable to be of a certain type?
eg
function foo(int $bar_int)
{
//code
} 8 2047
Hugh Oxford schreef:
I know I can force a variable passed to a function to be an object of
class bar
eg
function foo(bar $bar_object)
{
//code
}
but can I force a variable to be of a certain type?
eg
function foo(int $bar_int)
{
//code
}
Hi Hugh,
I am not aware of such a languageconstru ct.
You can of course test yourself what is passed:
function foo($bar_int){
if (!is_integer($b ar_int)){
trigger_error(" function foo accepts only integers.");
}
// code
}
For a list of all is_* checks: http://nl3.php.net/manual/en/function.is-array.php
On the left pane you'll see them all: is_double, is_bool, etc.
is_double, is_real, is_float are all the same. ;-)
Regards,
Erwin Moller
--
=============== =============
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
=============== =============
Hugh Oxford wrote:
I know I can force a variable passed to a function to be an object of
class bar
eg
function foo(bar $bar_object)
{
//code
}
but can I force a variable to be of a certain type?
eg
function foo(int $bar_int)
{
//code
}
You really can't. If you're using classes, you can require it to be an
object of a specific class, but not any of the basic types. PHP doesn't
support function overloading.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attgl obal.net
=============== ===
On Aug 5, 12:23*pm, Hugh Oxford <ares...@fas.co mwrote:
but can I force a variable to be of a certain type?
Of course you have tried
function bla(int $i)
and found that it did not work. The manual says about this:
"Type Hints can only be of the object and array (since PHP 5.1) type.
Traditional type hinting with int and string isn't supported." (http://
nl.php.net/manual/en/language.oop5.t ypehinting.php)
However, you can do the following:
function bla($i) {
assert(is_numer ic($i));
...
}
On Aug 5, 11:56 am, Sjoerd <sjoer...@gmail .comwrote:
On Aug 5, 12:23 pm, Hugh Oxford <ares...@fas.co mwrote:
but can I force a variable to be of a certain type?
Of course you have tried
function bla(int $i)
and found that it did not work. The manual says about this:
"Type Hints can only be of the object and array (since PHP 5.1) type.
Traditional type hinting with int and string isn't supported." (http://
nl.php.net/manual/en/language.oop5.t ypehinting.php)
However, you can do the following:
function bla($i) {
assert(is_numer ic($i));
...
}
From the PHP manual:
Assertions should be used as a debugging feature only. You may use
them for sanity-checks that test for conditions that should always be
TRUE and that indicate some programming errors if not or to check for
the presence of certain features like extension functions or certain
system limits and features.
Assertions should not be used for normal runtime operations like input
parameter checks. As a rule of thumb your code should always be able
to work correctly if assertion checking is not activated.
A better approach would be:
function bla($i) {
if (is_numeric($i) )
{
...
}
}
On Aug 5, 5:17 pm, Gordon <gordon.mc...@n tlworld.comwrot e:
On Aug 5, 11:56 am, Sjoerd <sjoer...@gmail .comwrote:
On Aug 5, 12:23 pm, Hugh Oxford <ares...@fas.co mwrote:
but can I force a variable to be of a certain type?
Of course you have tried
function bla(int $i)
and found that it did not work. The manual says about this:
"Type Hints can only be of the object and array (since PHP 5.1) type.
Traditional type hinting with int and string isn't supported." (http://
nl.php.net/manual/en/language.oop5.t ypehinting.php)
However, you can do the following:
function bla($i) {
assert(is_numer ic($i));
...
}
From the PHP manual:
Assertions should be used as a debugging feature only. You may use
them for sanity-checks that test for conditions that should always be
TRUE and that indicate some programming errors if not or to check for
the presence of certain features like extension functions or certain
system limits and features.
Assertions should not be used for normal runtime operations like input
parameter checks. As a rule of thumb your code should always be able
to work correctly if assertion checking is not activated.
A better approach would be:
function bla($i) {
if (is_numeric($i) )
{
...
}
}
Yes - this rather looks like you are creating errors instead of
handling them. If you really want your PHP to behave like a strongly
typed language then have a look at PHPlint, but really the solution is
to understand how dynamic typing works and apply that as part of the
validation at the point where data enters and leaves your code.
C.
Gordon wrote:
On Aug 5, 11:56 am, Sjoerd <sjoer...@gmail .comwrote:
>On Aug 5, 12:23 pm, Hugh Oxford <ares...@fas.co mwrote:
>>but can I force a variable to be of a certain type?
Of course you have tried function bla(int $i) and found that it did not work. The manual says about this: "Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported." (http:// nl.php.net/manual/en/language.oop5.t ypehinting.php)
However, you can do the following:
function bla($i) { assert(is_numer ic($i)); ...
}
From the PHP manual:
Assertions should be used as a debugging feature only. You may use
them for sanity-checks that test for conditions that should always be
TRUE and that indicate some programming errors if not or to check for
the presence of certain features like extension functions or certain
system limits and features.
Assertions should not be used for normal runtime operations like input
parameter checks. As a rule of thumb your code should always be able
to work correctly if assertion checking is not activated.
A better approach would be:
function bla($i) {
if (is_numeric($i) )
{
...
}
}
Gordon,
What is the URL of the quote above from the manual?
Before I started doing unit tests, my code was full of such assertions,
used for error checking. I'm wondering what the argument against using
them was?
I mean, yes, they are for debugging, but at least for the projects I was
working on, we were forever debugging.
-- lawrence
Lawrence Krubner wrote:
Gordon wrote:
>On Aug 5, 11:56 am, Sjoerd <sjoer...@gmail .comwrote:
>>On Aug 5, 12:23 pm, Hugh Oxford <ares...@fas.co mwrote:
but can I force a variable to be of a certain type? Of course you have tried function bla(int $i) and found that it did not work. The manual says about this: "Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported." (http:// nl.php.net/manual/en/language.oop5.t ypehinting.php)
However, you can do the following:
function bla($i) { assert(is_numer ic($i)); ...
}
From the PHP manual:
Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.
Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.
A better approach would be:
function bla($i) { if (is_numeric($i) ) { ... } }
Gordon,
What is the URL of the quote above from the manual?
Before I started doing unit tests, my code was full of such assertions,
used for error checking. I'm wondering what the argument against using
them was?
I mean, yes, they are for debugging, but at least for the projects I was
working on, we were forever debugging.
-- lawrence
Did you even look at the manual - like maybe under assert()?
And no, it's not a good idea to leave such code in the system. It
causes unnecessary overhead during normal operation. Also, depending on
the assert_options( ) setting, the script could terminate prematurely -
ok in a test environment but not a live one.
And if you have a good design and quality code to start with, you won't
be "forever debugging". But too many projects don't even have a design
to start - just some nebulous idea of what they want. So you don't
stand a chance, even if you have good programmers.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attgl obal.net
=============== ===
On Aug 7, 8:06 am, Lawrence Krubner <lawre...@krubn er.comwrote:
Gordon wrote:
On Aug 5, 11:56 am, Sjoerd <sjoer...@gmail .comwrote:
On Aug 5, 12:23 pm, Hugh Oxford <ares...@fas.co mwrote:
>but can I force a variable to be of a certain type?
Of course you have tried
function bla(int $i)
and found that it did not work. The manual says about this:
"Type Hints can only be of the object and array (since PHP 5.1) type.
Traditional type hinting with int and string isn't supported." (http://
nl.php.net/manual/en/language.oop5.t ypehinting.php)
However, you can do the following:
function bla($i) {
assert(is_numer ic($i));
...
}
From the PHP manual:
Assertions should be used as a debugging feature only. You may use
them for sanity-checks that test for conditions that should always be
TRUE and that indicate some programming errors if not or to check for
the presence of certain features like extension functions or certain
system limits and features.
Assertions should not be used for normal runtime operations like input
parameter checks. As a rule of thumb your code should always be able
to work correctly if assertion checking is not activated.
A better approach would be:
function bla($i) {
if (is_numeric($i) )
{
...
}
}
Gordon,
What is the URL of the quote above from the manual?
Before I started doing unit tests, my code was full of such assertions,
used for error checking. I'm wondering what the argument against using
them was?
I mean, yes, they are for debugging, but at least for the projects I was
working on, we were forever debugging.
-- lawrence
http://www.php.net/assert This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matthew Sims |
last post by:
Is it possible to force a download without using the readfile
function?
My website setup consists of my server that serves the web pages plus
a high-speed file server elsewhere on the internet that servers my
files. I'm currently using header("Location: abc.com") to redirect my
users to this other site but can't force a download.
I can get it to work using readfile() but that means the file moves
from the high-speed server through my...
|
by: zhou |
last post by:
Hi there,
We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes
when I try using std:find() on vector. Since vector has no member function find() and I have to use std::find(). The
linker fails on unsatisified symbols in find().
I think this is because find() is a template function and I have to force instantiation. However, I donot know the
correct syntax of doing so....
|
by: Curious Angel |
last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to
force the file to be saved, and in any event _not_ opened.
Since the PDF will be in his cache in any event, I would just as soon place the
employer in control of what directory he wishes to save it in, and there are two
salient reasons for this:
1. I want him to OWN the document
2. I want him to FIND the document, quickly, on his hard drive
In any...
|
by: joltman |
last post by:
OK, this is kind of hard to explain, so I'll do my best:
I have a form where I have a row where there could be multiple entries,
so I have a link where it will dynamically add another row like it, and
this can happen as many times as you click on the link. This row
consists of 2 drop-down boxes. Depending on the selection of the first
drop-down box, the second drop-down box is either disabled or enabled.
The problem is on these dynamically...
|
by: Keith H |
last post by:
I'm looking for a way to force the user to re-authenticate with their Windows
username/password/domain after clicking the submit button on an ASP.NET page.
This is for an internal application.
Does anyone know if/how this can be done?
| |
by: Mark A |
last post by:
DB2 ESE 8.2.3 (FP10) for Linux
We are experiencing a connection hang of 10 - 15 minutes in the following
HADR and automatic client reroute scenario:
01 server is primary database
02 server is standby database
a. applications connected to database on 01 server
b. shutdown 01 server
|
by: comp.lang.php |
last post by:
class ReportGenerator {
function ReportGenerator() {}
/**
* Generate the HTTP headers necessary for this file type. Can be
called statically
*
|
by: bob |
last post by:
Now this ought to be a simple matter. But nothing's simple in the Net
world, I'm finding.
In vb6 you could use "!" to force text to upper case in the format
function. I've searched the vb.net help system and can't find any help
on formatting text. There's plenty of help formatting numbers, dates,
and times, though.
I'm in the phase of converting from 6 to Net and it seems that even the
simplest thing is very, very complicated. And it...
|
by: jinnareddy |
last post by:
Hi,
I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL encoding too, but with no success.
Can someone provide details on how to handle the 2-byte char URLs and download the files?
Appreciate your suggestions/help in resolving it.
Here is my code:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |