473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How about const supporting array in future?

Array is the very basic type in php i think. So why const keyword
doesn't support such defination grammar?

class Date
{
const char3 = array(1 ='Jan', ...); // ??
}

So how can i do somthing like c++'s enum?

class Data
{
const Jan = 1;
//...
}

is poor if i want to chanslate number to month name.

Aug 2 '07 #1
8 1917
On Aug 2, 12:55 pm, Yarco <yarc...@gmail.comwrote:
Array is the very basic type in php i think. So why const keyword
doesn't support such defination grammar?

class Date
{
const char3 = array(1 ='Jan', ...); // ??

}

So how can i do somthing like c++'s enum?

class Data
{
const Jan = 1;
//...

}

is poor if i want to chanslate number to month name.
Well if you can't have const array, do a public static property
instead
class Data {
public static $MONTH_NAMES = array('January', ...);
}

Hendri Kurniawan

Aug 2 '07 #2
Yes, it can be done like this.
But cause $MONTH_NAMES is variable, someone could change the value.

// Data.cls.php
class Date {
public static $MONTH_NAMES = array('January', ...);
}

// Plugin.php
Date::$MONTH_NAMES = null; // someone don't know the meaning of the
variable

// Template.php
print Date::$MONTH_NAMES[1]; // then your code doesn't work

On Aug 2, 11:01 am, Hendri Kurniawan <hckurnia...@gmail.comwrote:
On Aug 2, 12:55 pm, Yarco <yarc...@gmail.comwrote:
Array is the very basic type in php i think. So why const keyword
doesn't support such defination grammar?
class Date
{
const char3 = array(1 ='Jan', ...); // ??
}
So how can i do somthing like c++'s enum?
class Data
{
const Jan = 1;
//...
}
is poor if i want to chanslate number to month name.

Well if you can't have const array, do a public static property
instead
class Data {
public static $MONTH_NAMES = array('January', ...);

}

Hendri Kurniawan

Aug 2 '07 #3
Yarco wrote:
Yes, it can be done like this.
But cause $MONTH_NAMES is variable, someone could change the value.

// Data.cls.php
class Date {
public static $MONTH_NAMES = array('January', ...);
}

// Plugin.php
Date::$MONTH_NAMES = null; // someone don't know the meaning of the
variable

// Template.php
print Date::$MONTH_NAMES[1]; // then your code doesn't work

On Aug 2, 11:01 am, Hendri Kurniawan <hckurnia...@gmail.comwrote:
>On Aug 2, 12:55 pm, Yarco <yarc...@gmail.comwrote:
>>Array is the very basic type in php i think. So why const keyword
doesn't support such defination grammar?
class Date
{
const char3 = array(1 ='Jan', ...); // ??
}
So how can i do somthing like c++'s enum?
class Data
{
const Jan = 1;
//...
}
is poor if i want to chanslate number to month name.
Well if you can't have const array, do a public static property
instead
class Data {
public static $MONTH_NAMES = array('January', ...);

}

Hendri Kurniawan

Make it private and don't change it in the class (requires PHP 5.x,
which people should be on now - or at least soon).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 2 '07 #4
Yes, they could work if we do our effort.
We could add final keyword to forbidden someone to change this
class...

final class Date
{
private static $month_names = array(...);

public static getMonthName($i)
{
return $month_names[$i];
}
}

But const array would keep it simple.
On Aug 2, 11:49 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Yarco wrote:
Yes, it can be done like this.
But cause $MONTH_NAMES is variable, someone could change the value.
// Data.cls.php
class Date {
public static $MONTH_NAMES = array('January', ...);
}
// Plugin.php
Date::$MONTH_NAMES = null; // someone don't know the meaning of the
variable
// Template.php
print Date::$MONTH_NAMES[1]; // then your code doesn't work
On Aug 2, 11:01 am, Hendri Kurniawan <hckurnia...@gmail.comwrote:
On Aug 2, 12:55 pm, Yarco <yarc...@gmail.comwrote:
>Array is the very basic type in php i think. So why const keyword
doesn't support such defination grammar?
class Date
{
const char3 = array(1 ='Jan', ...); // ??
}
So how can i do somthing like c++'s enum?
class Data
{
const Jan = 1;
//...
}
is poor if i want to chanslate number to month name.
Well if you can't have const array, do a public static property
instead
class Data {
public static $MONTH_NAMES = array('January', ...);
}
Hendri Kurniawan

Make it private and don't change it in the class (requires PHP 5.x,
which people should be on now - or at least soon).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Aug 2 '07 #5
Yarco wrote:
So how can i do somthing like c++'s enum?
Only for global scope.

----------------enum.inc.php--------------------------------->8-------------------

<?php
/**
* Copyright (c) 2006 by Alexey Kulentsov
*
* usage example:
* enum(1,'one','two','three',5,'five','six');
*
* History:
* 2006-02-23 started
* 2007-05-31 optimized
*/

function enum()
{
$counter=0;
$retval=array();

foreach(func_get_args() as $arg)
if(gettype($arg)=='integer')
$counter=$arg;
else
define($retval[$counter++]=$arg,$counter);

return $retval;
}

?>
------------------------------------------------------------->8-------------------
Aug 2 '07 #6
On 02.08.2007 04:55 Yarco wrote:
Array is the very basic type in php i think. So why const keyword
doesn't support such defination grammar?

class Date
{
const char3 = array(1 ='Jan', ...); // ??
}

So how can i do somthing like c++'s enum?

class Data
{
const Jan = 1;
//...
}

is poor if i want to chanslate number to month name.
The reason of disabling arrays was that parser wasn't able to parse
"someConst[key]" correctly.

http://bugs.php.net/bug.php?id=25816

As to enums, they are used in C to give symbolic names to integer
constants. In php constants don't have to be integer, that is we can
simply use strings instead of symbolic names:

// c way

const BY_NAME = 1;
....
if($sort_mode == BY_NAME) ...
// php way

if($sort_mode == 'BY_NAME') ...
ruby's interned strings (:BY_NAME) would be even better, unfortunately
php doesn't support this

--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Aug 2 '07 #7
Yarco wrote:
But cause $MONTH_NAMES is variable, someone could change the value.
If you overload the class' __set() function you can effectively make it
read-only. (Assuming you're using PHP 5.)

Better though:

function get_month_name ($n, $fmt='M')
{
$d = mktime(12, 0, 0, $n, 1, 1970);
return date($fmt, $d);
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 42 days, 12:20.]

PHP Encryption Class
http://tobyinkster.co.uk/blog/2007/0...ryption-class/
Aug 2 '07 #8
Well...Yes....but nothing is better than use const array.

I've already done such things:
$buf = range(0, 12); unset($buf[0]);
array_walk($buf, create_function('&$v, $k', '$v = date("M",
strtotime("2007-$k-01"));'));
print_r($buf);

.......what a pity php doesn't support const array...

On Aug 2, 4:44 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Yarco wrote:
But cause $MONTH_NAMES is variable, someone could change the value.

If you overload the class' __set() function you can effectively make it
read-only. (Assuming you're using PHP 5.)

Better though:

function get_month_name ($n, $fmt='M')
{
$d = mktime(12, 0, 0, $n, 1, 1970);
return date($fmt, $d);
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 42 days, 12:20.]

PHP Encryption Class
http://tobyinkster.co.uk/blog/2007/0...ryption-class/

Aug 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Gernot Frisch | last post by:
void foo(int* p) {} main() { foo( {1,2,3} ); } How can I do this? I have a class, that expects this as a constructor:
7
by: BigMan | last post by:
Which is the preferred way to create a const container in general: 1. container_type< value_type > const; or 2. container_type< value_type const >?
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
0
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
13
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.