All three of the following classes give "compile-time" errors. It
hinders me from doing what I want to do... And it seems there is no way
around it. But, are each of these cases examples of how the language
should behave? Are any of the examples a case of PHP being
"incorrect?"
class MyClass
{
static $x = 2;
static $y = self::$x; // error
}
class MyClass2
{
var $x = 2;
static $y = $x; // error
}
class MyClass3
{
const a = array(1,2); // error
}
-- http://www.douglassdavis.com/ 14 1212 www.douglassdavis.com wrote: All three of the following classes give "compile-time" errors. It hinders me from doing what I want to do... And it seems there is no way around it. But, are each of these cases examples of how the language should behave? Are any of the examples a case of PHP being "incorrect?"
I don't know PHP, but I believe these are bugs in your code:
class MyClass { static $x = 2; static $y = self::$x; // error }
there is no instantiation of the class--there is no "self" object.
Did you mean "static $y = $x"?
class MyClass2 { var $x = 2; static $y = $x; // error }
In this case, since no object was created, there is no $x to assign into
$y.
class MyClass3 { const a = array(1,2); // error }
I defer this to someone who knows PHP, but if its semantics are
comparable to Java and C#, does "array(1,2)" actually create an instance
of an array? Based on the pattern of the errors above, I'd suggest you
review the concepts of object instantiation and class/instance member
variables.
raf
raf wrote: www.douglassdavis.com wrote: All three of the following classes give "compile-time" errors. It hinders me from doing what I want to do... And it seems there is no way around it. But, are each of these cases examples of how the language should behave? Are any of the examples a case of PHP being "incorrect?"
I don't know PHP, but I believe these are bugs in your code:
class MyClass { static $x = 2; static $y = self::$x; // error }
there is no instantiation of the class--there is no "self" object. Did you mean "static $y = $x"?
self:: isn't $this-> self:: just refers to the current class, just
like putting the class name there. In PHP, you always have to prefix a
class variable with something, even if you are writing code in that
class. class MyClass2 { var $x = 2; static $y = $x; // error }
In this case, since no object was created, there is no $x to assign into $y.
yes... this one doesn't make sense. class MyClass3 { const a = array(1,2); // error }
I defer this to someone who knows PHP, but if its semantics are comparable to Java and C#, does "array(1,2)" actually create an instance of an array? Based on the pattern of the errors above, I'd suggest you review the concepts of object instantiation and class/instance member variables.
yes array creates an instance of array.
to the PHP folks out there:
My delima is this... let's say you wanted to do something like this in
a class, and you needed all 3 of these vars to be defined.
class MyClass4
{
var $one = array(1,2);
var $two = array(3,4);
var $three= array($one, $two); // a two dimensional array.
// ... more code below
}
But you knew that these would always remain the same, and there was no
need for putting them in every class. Meaning, if they are not static,
it's just a waste of space.
Also, there are no static initializer blocks like java.
Is there a correct way to define this in the class this without wasting
space? www.douglassdavis.com wrote: PHP
Is it too late to switch to Ruby?
That's not a joke; it's an excellent language that will lead to very elegant
solutions here.
My delima is this... let's say you wanted to do something like this in a class, and you needed all 3 of these vars to be defined.
class MyClass4 { var $one = array(1,2); var $two = array(3,4); var $three= array($one, $two); // a two dimensional array. // ... more code below }
But you knew that these would always remain the same, and there was no need for putting them in every class. Meaning, if they are not static, it's just a waste of space.
Premature optimization is the root of all evil. In this case, if you need an
object, even as a compilable comment, create an object.
Put another way, it's easier to make clean code fast than make fast code
clean. Clean code is easy to read and understand. Create an object that
duplicates its instances of these heavy members. Then get your program to
work, and then think about _explicitely_ replacing the heavy members with
references to shared single instances of their data.
Also, there are no static initializer blocks like java.
Write them yourself. In Ruby, they would be...
@@one ||= [1,2]
one is a class variable, and if it's not assigned yet then create an array
and plug it in.
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
I think you get the compile time errors because the class properties can
be initialized with a constant expression only. Calling a function or
another variable is not seen as a constant expression, even if the
result is a constant.
This is a language thing, so I think you will have to live with it.
What you can do is creating some "root" instance of your class and set
the properties in the constructor. This also has the advantage of a more
testable object model.
Best regards www.douglassdavis.com wrote: raf wrote:
www.douglassdavis.com wrote:
All three of the following classes give "compile-time" errors. It hinders me from doing what I want to do... And it seems there is no way around it. But, are each of these cases examples of how the language should behave? Are any of the examples a case of PHP being "incorrect?"
I don't know PHP, but I believe these are bugs in your code: class MyClass { static $x = 2; static $y = self::$x; // error }
there is no instantiation of the class--there is no "self" object. Did you mean "static $y = $x"?
self:: isn't $this-> self:: just refers to the current class, just like putting the class name there. In PHP, you always have to prefix a class variable with something, even if you are writing code in that class. class MyClass2 { var $x = 2; static $y = $x; // error }
In this case, since no object was created, there is no $x to assign into $y.
yes... this one doesn't make sense.
class MyClass3 { const a = array(1,2); // error }
I defer this to someone who knows PHP, but if its semantics are comparable to Java and C#, does "array(1,2)" actually create an instance of an array? Based on the pattern of the errors above, I'd suggest you review the concepts of object instantiation and class/instance member variables.
yes array creates an instance of array.
to the PHP folks out there:
My delima is this... let's say you wanted to do something like this in a class, and you needed all 3 of these vars to be defined.
class MyClass4 { var $one = array(1,2); var $two = array(3,4); var $three= array($one, $two); // a two dimensional array. // ... more code below }
But you knew that these would always remain the same, and there was no need for putting them in every class. Meaning, if they are not static, it's just a waste of space.
Also, there are no static initializer blocks like java.
Is there a correct way to define this in the class this without wasting space? www.douglassdavis.com (do**@douglassdavis.com) wrote:
: static $y = self::$x; // error
I wonder if that should be $self::x;
--
This programmer available for rent.
Phlip wrote: www.douglassdavis.com wrote:
PHP
Is it too late to switch to Ruby?
That's not a joke; it's an excellent language that will lead to very elegant solutions here.
I'll check it out, but this kinda scared me:
Features of Ruby
* Ruby has simple syntax, partially inspired by Eiffel and Ada.
IMO, there's a reason why people aren't flocking to use Eiffel and
Ada... Although Ruby it should be interesting, so I'll take a look.
[my feeble attempts to debug PHP code snipped] My delima is this... let's say you wanted to do something like this in a class, and you needed all 3 of these vars to be defined.
class MyClass4 { var $one = array(1,2); var $two = array(3,4); var $three= array($one, $two); // a two dimensional array. // ... more code below }
But you knew that these would always remain the same, and there was no need for putting them in every class. Meaning, if they are not static, it's just a waste of space.
First, your descriptive naming conventions don't help in understanding
the intent of your code.
Second, what do you mean "always remain the same?" Do you mean that
every single instance of MyClass4 shares the exact same single instance
of each of those arrays?
If so, might it be worth considering factoring out the array and using a
Singleton pattern for whatever they represent?
If not, rethink your design. And your naming conventions.'
Also, there are no static initializer blocks like java.
Makes me wonder why people flock to use PHP then ;-)
Is there a correct way to define this in the class this without wasting space?
See my suggestion above...
raf www.douglassdavis.com wrote: I'll check it out, but this kinda scared me:
Features of Ruby
* Ruby has simple syntax, partially inspired by Eiffel and Ada.
IMO, there's a reason why people aren't flocking to use Eiffel and Ada... Although Ruby it should be interesting, so I'll take a look.
I don't know who wrote that. Ruby was invented to compete directly,
head-to-head, with both Perl and Smalltalk at the same time. That's an
incredible stretch, and Matz did it by bowing to Perl's kewtzey /regexp/
syntax, with magic $1 and $2 variables for the match results. Matz then
fixed this otherwise ridiculous system by making $ the prefix for global
variables _only_. That instantly makes $1 and $2 work, because they are
nothing but stereotypical global variables.
Ruby competes with Smalltalk by making everything an object, including
classes, and making all objects indefinitely extensible. Oh, and block
closures so you don't need zillions of lines of code to get trivial things
done. Block closures permit local variables to have the narrowest scope
possible over indefinite lifespans, enhancing encapsulation without the need
for excessive plumbing to route those variables into the called-back block
where they are ultimately used.
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!! www.douglassdavis.com wrote: class MyClass2 { var $x = 2; static $y = $x; // error }
$x does not exist until instantiation, so you should assign $y in the
constructor:
class MyClass2 {
var $x = 2;
static $y;
function __construct() {
$this->y = $this->x;
}
}
The same principle applies to the previous MyClass code, where $this->x
should be replaced by self::$x.
class MyClass3 { const a = array(1,2); // error }
From http://www.php.net/manual/en/language.constants.php:
"Only scalar data (boolean, integer, float and string) can be contained in
constants."
JW
Janwillem Borleffs wrote: class MyClass2 { var $x = 2; static $y;
function __construct() { $this->y = $this->x; } }
Bad example as $y is set as a normal class property instead of a static. In
general, statics should be assigned with hardcoded values if you want to
access them directly without creating an instance of the class first.
JW
Phlip wrote: www.douglassdavis.com wrote:
I'll check it out, but this kinda scared me:
Features of Ruby
* Ruby has simple syntax, partially inspired by Eiffel and Ada.
IMO, there's a reason why people aren't flocking to use Eiffel and Ada... Although Ruby it should be interesting, so I'll take a look.
I don't know who wrote that. Ruby was invented to compete directly, head-to-head, with both Perl and Smalltalk at the same time. That's an incredible stretch, and Matz did it by bowing to Perl's kewtzey /regexp/ syntax, with magic $1 and $2 variables for the match results. Matz then fixed this otherwise ridiculous system by making $ the prefix for global variables _only_. That instantly makes $1 and $2 work, because they are nothing but stereotypical global variables.
Ruby competes with Smalltalk by making everything an object, including classes, and making all objects indefinitely extensible. Oh, and block closures so you don't need zillions of lines of code to get trivial things done. Block closures permit local variables to have the narrowest scope possible over indefinite lifespans, enhancing encapsulation without the need for excessive plumbing to route those variables into the called-back block where they are ultimately used.
And I guess C++ is not worth the effort.
raf wrote: First, your descriptive naming conventions don't help in understanding the intent of your code.
the only intent was to demonstrate a language concept. :)
Second, what do you mean "always remain the same?" Do you mean that every single instance of MyClass4 shares the exact same single instance of each of those arrays?
If so, might it be worth considering factoring out the array and using a Singleton pattern for whatever they represent?
sure.. that's an option. Since it's just a bunch of data that doesn't
change, I was wondering if i could use member variables to represent
them, rather than classes... But, good suggestion though.
If not, rethink your design. And your naming conventions.'
That was only an example :) The real code is too long. Also, there are no static initializer blocks like java.
Makes me wonder why people flock to use PHP then ;-)
Because we actually like programming in PHP :) www.douglassdavis.com wrote: raf wrote:
First, your descriptive naming conventions don't help in understanding the intent of your code.
the only intent was to demonstrate a language concept. :)
Unfortunately your fragment, though just a small demonstration, shows
the potential shortcoming of using code as a "design artifact"--it
showed me what you did, but gave me no insight in what your intent was.
did those arrays represent a game board and the classes represented
player controllers? Were they tables in a restaurant and the classes
represented bus boys? The PHP problem was one level of concern, but if
we had more insight into what you were trying to do it would make things
easier.
names matter.
raf
sure.. that's an option. Since it's just a bunch of data that doesn't change, I was wondering if i could use member variables to represent them, rather than classes... But, good suggestion though.
"bunch of data"? where the abstraction, man? "I've got a loverly bunch
of data... here they are, standing in a row"
What does the data represent?
That was only an example :) The real code is too long.
no need to post the entire codebase, but just two or three sentences
describing what the fragment represented and meaningful names... that
should be easy, no? Makes me wonder why people flock to use PHP then ;-)
Because we actually like programming in PHP :)
Just like Eiffel and Ada programmers, I suppose.
raf
raf wrote:
.... represented bus boys? The PHP problem was one level of concern, but if we had more insight into what you were trying to do it would make things easier.
names matter.
raf
sure.. that's an option. Since it's just a bunch of data that doesn't change, I was wondering if i could use member variables to represent them, rather than classes... But, good suggestion though.
"bunch of data"? where the abstraction, man? "I've got a loverly bunch of data... here they are, standing in a row"
exactly.
And thanks for solving the problem, that you knew nothing about. :-| Because we actually like programming in PHP :)
Just like Eiffel and Ada programmers, I suppose.
Ok, I'll give you Eiffel... Although many of the users are academic,
rather than real world. I remember I had to take a class on it. As
far as Ada goes, there's a significant number of people who program in
Ada because they -have- to.
Also, there is a difference between being "correct" and being
"practical." ;-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christian Seberino |
last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python?
Python's design is godly. I'm wondering if Ruby's is godly too.
I've heard it has solid OOP design but then...
|
by: Andrea Griffini |
last post by:
I did it.
I proposed python as the main language for our next CAD/CAM
software because I think that it has all the potential needed
for it. I'm not sure yet if the decision will get through, but...
|
by: Steven T. Hatton |
last post by:
I came across this while looking for information on C++ and CORBA:
http://www.zeroc.com/ice.html. It got me to wondering why I need two
different languages in order to write distributed computing...
|
by: Kay Schluehr |
last post by:
No good news for scripting-language fans:
http://www.phpmag.net/itr/news/psecom,id,23284,nodeid,113.html
Regards
Kay
|
by: pcmanlin |
last post by:
As I know java has many UML tools to design for its OO feature, is
there any tools or good concept for Python project Modeling?
|
by: Raymond Hettinger |
last post by:
Proposal
--------
I am gathering data to evaluate a request for an alternate version of
itertools.izip() with a None fill-in feature like that for the built-in
map() function:
>>> map(None,...
|
by: toolmaster |
last post by:
Since many of the modern computer languages have built-in namespace
features, I can't understand why not add this feature into standard C.
I've heard many people complain of the lacking of...
|
by: Reidar |
last post by:
Is it possible to have the source in one window and the design in another
window and perhaps the code in a third window?
reidarT
|
by: YellowFin Announcements |
last post by:
Introduction
Usability and relevance have been identified as the major factors
preventing mass adoption of
Business Intelligence applications. What we have today are traditional
BI tools that...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
| |