473,406 Members | 2,620 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,406 software developers and data experts.

Class question. Where am I going wrong?

Hi everyone,

I figured by doing a larger project, I could start to learn how objects
and classes work in PHP, but I am running into a problem.

I am trying to write a sudoku generator. I was going to make a cell
class to hold the potential values of a cell...

class cell {
public $potentialValues = array(1,2,3,4,5,6,7,8,9);
}

Then a row class to hold and run functions of the cells in a row...

class row {
public $cell1 = new cell();
public $cell2 = new cell();
public $cell3 = new cell();
public $cell4 = new cell();
public $cell5 = new cell();
public $cell6 = new cell();
public $cell7 = new cell();
public $cell8 = new cell();
public $cell9 = new cell();
}

And finally a matrix class to hold all the rows...

class matrix {
public $row1 = &new row();
public $row2 = &new row();
public $row3 = &new row();
public $row4 = &new row();
public $row5 = &new row();
public $row6 = &new row();
public $row7 = &new row();
public $row8 = &new row();
public $row9 = &new row();
}

The problem is is that I guess I cannot use objects within another
class since this code errors. (Parse error: syntax error, unexpected
T_NEW in yadda/yadda/yadda.php). Could someone take a second to set me
right and kindly point me in the right direction? I have a feeling I
am just not looking at this problem correctly.

Thanks, Adam

Oct 6 '06 #1
6 1301

Adam napisal(a):
class matrix {
public $row1 = &new row();
public $row2 = &new row();
public $row3 = &new row();
public $row4 = &new row();
public $row5 = &new row();
public $row6 = &new row();
public $row7 = &new row();
public $row8 = &new row();
public $row9 = &new row();
}

The problem is is that I guess I cannot use objects within another
class since this code errors. (Parse error: syntax error, unexpected
T_NEW in yadda/yadda/yadda.php). Could someone take a second to set me
right and kindly point me in the right direction? I have a feeling I
am just not looking at this problem correctly.
A default class property must be string, number or array. "new"
operator is not allowed.
Why are creating so many properties instead an array of them? Try:

class Matrix {
private $rows = array();
public function __construct($howmany)
{
for ($i=0;$i<$howmany;$i++) {
$this->rows[$i] =new Row();
}
}
}

Oct 6 '06 #2

Mateusz Markowski wrote:
Adam napisal(a):
class matrix {
public $row1 = &new row();
public $row2 = &new row();
public $row3 = &new row();
public $row4 = &new row();
public $row5 = &new row();
public $row6 = &new row();
public $row7 = &new row();
public $row8 = &new row();
public $row9 = &new row();
}

The problem is is that I guess I cannot use objects within another
class since this code errors. (Parse error: syntax error, unexpected
T_NEW in yadda/yadda/yadda.php). Could someone take a second to set me
right and kindly point me in the right direction? I have a feeling I
am just not looking at this problem correctly.

A default class property must be string, number or array. "new"
operator is not allowed.
Why are creating so many properties instead an array of them? Try:

class Matrix {
private $rows = array();
public function __construct($howmany)
{
for ($i=0;$i<$howmany;$i++) {
$this->rows[$i] =new Row();
}
}
}
I dont know why I am not using an array. I think I am getting confused
on objects and I am losing my mind :) What is the reasoning behind why
default class properties only being a string, number, or array? I think
knowing that would help me understand better. Thanks for your help
Mateusz!

Oct 6 '06 #3
Adam wrote:
Mateusz Markowski wrote:
>Adam napisal(a):
>>class matrix {
public $row1 = &new row();
public $row2 = &new row();
public $row3 = &new row();
public $row4 = &new row();
public $row5 = &new row();
public $row6 = &new row();
public $row7 = &new row();
public $row8 = &new row();
public $row9 = &new row();
}

The problem is is that I guess I cannot use objects within another
class since this code errors. (Parse error: syntax error, unexpected
T_NEW in yadda/yadda/yadda.php). Could someone take a second to set me
right and kindly point me in the right direction? I have a feeling I
am just not looking at this problem correctly.
A default class property must be string, number or array. "new"
operator is not allowed.
Why are creating so many properties instead an array of them? Try:

class Matrix {
private $rows = array();
public function __construct($howmany)
{
for ($i=0;$i<$howmany;$i++) {
$this->rows[$i] =new Row();
}
}
}

I dont know why I am not using an array. I think I am getting confused
on objects and I am losing my mind :) What is the reasoning behind why
default class properties only being a string, number, or array? I think
knowing that would help me understand better. Thanks for your help
Mateusz!
The manual http://www.php.net/manual/en/language.oop5.basic.php actually
says:

"The default value must be a constant expression, not (for example) a
variable, a class member or a function call."

I don't know the reason, but I observe that this way, these initial
values will be the same in every instance of the class. If you were
allowed variables, function calls or new (which is a disguised function
call) they might have different values for every object of the class.

There's nothing wrong with this, but you can get that effect by setting
the value in the constructor, so it is useful to have variable
initialisation serving a different purpose.

Colin
Oct 6 '06 #4
I banged my head against the php wall for a while when I wrote my first
simple OOP program in php; all because I was using one underscore when
writing __construct rather than two! I am guessing this frustration
happens all the time, and I couldn't believe the book I was using did
not explicitly state this. Is there a reason it was specified with two
underscores?

Eric

Oct 6 '06 #5
Adam wrote:
Mateusz Markowski wrote:
>>Adam napisal(a):
>>>class matrix {
public $row1 = &new row();
public $row2 = &new row();
public $row3 = &new row();
public $row4 = &new row();
public $row5 = &new row();
public $row6 = &new row();
public $row7 = &new row();
public $row8 = &new row();
public $row9 = &new row();
}

The problem is is that I guess I cannot use objects within another
class since this code errors. (Parse error: syntax error, unexpected
T_NEW in yadda/yadda/yadda.php). Could someone take a second to set me
right and kindly point me in the right direction? I have a feeling I
am just not looking at this problem correctly.

A default class property must be string, number or array. "new"
operator is not allowed.
Why are creating so many properties instead an array of them? Try:

class Matrix {
private $rows = array();
public function __construct($howmany)
{
for ($i=0;$i<$howmany;$i++) {
$this->rows[$i] =new Row();
}
}
}


I dont know why I am not using an array. I think I am getting confused
on objects and I am losing my mind :) What is the reasoning behind why
default class properties only being a string, number, or array? I think
knowing that would help me understand better. Thanks for your help
Mateusz!
Default values must be constants. If you want to execute code as above,
simply put it in the constructor for the class. That's what it's
designed for.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 6 '06 #6


e_*******@hotmail.com wrote:
I banged my head against the php wall for a while when I wrote my first
simple OOP program in php; all because I was using one underscore when
writing __construct rather than two! I am guessing this frustration
happens all the time, and I couldn't believe the book I was using did
not explicitly state this. Is there a reason it was specified with two
underscores?

Eric
Maybe because some people were using _xxx to emulate private properties/methods
when those where not available yet.

function _blaFoo() {}

Snef.
Oct 6 '06 #7

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

Similar topics

5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
6
by: Jeff Thies | last post by:
I have a number of elements of "some-class". I'd like to change the styles of some-class: from ..some-class{color: red; display: block} to
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
8
by: DaKoadMunky | last post by:
Say I have the following... class Foo { public: Foo() : bar(0) {} void SetBar(int);
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
17
by: wackyphill | last post by:
If you have a function that basically creates an object/struct that holds data only in a function and wish to return the object/struct from the function which is faster a class object or struct? ...
9
by: needin4mation | last post by:
I have a .aspx file and it's src. I also have a third file, a class that the src references. Everything is in the same directory/folder. Everything has the same namespace. How do I reference the...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
4
by: Adam Lanier | last post by:
Relatively new to python development and I have a general question regarding good class design. Say I have a couple of classes: Class Foo: params = __init__( self, param ): ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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
jinu1996
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...
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.