Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with classes

Jag
Guest
 
Posts: n/a
#1: Sep 20 '06

Hello there!
At the beginning, I'm apologize
for my weak english skills.
I got a problem with the class
which I include from the other
file. It's hard to me to explain
it clearly, so I just give you
files:

index.php:

<?php
include("someclass.inc.php");
$c=new classA();
$c->SayHallo();
include("another.php");
?>

comeclass.inc.php:

<?php
class classA()
{
function __construct()
{
echo 'Constructing...';
}
function SayHello()
{
echo "Hello, cruel world.";
}
}
?>

another.php:

<?php

First try:
$c->SayHello(); // error :/

Second try:
$c=new classA(); // ALMOST works. It
$c->SayHallo(); // defines second
// instance of the
// class ClassA.
// Unfortunetly, I
// need to work on
// the same instance.
?>

Hope somebody helps me - how to define
class and work with one and the same
instance?

--
best regards
Jag



Jerry Stuckle
Guest
 
Posts: n/a
#2: Sep 20 '06

re: Problem with classes


Jag wrote:
Quote:
Hello there!
At the beginning, I'm apologize
for my weak english skills.
I got a problem with the class
which I include from the other
file. It's hard to me to explain
it clearly, so I just give you
files:
>
index.php:
>
<?php
include("someclass.inc.php");
$c=new classA();
$c->SayHallo();
include("another.php");
?>
>
comeclass.inc.php:
>
<?php
class classA()
{
function __construct()
{
echo 'Constructing...';
}
function SayHello()
{
echo "Hello, cruel world.";
}
}
?>
>
another.php:
>
<?php
>
First try:
$c->SayHello(); // error :/
>
Second try:
$c=new classA(); // ALMOST works. It
$c->SayHallo(); // defines second
// instance of the
// class ClassA.
// Unfortunetly, I
// need to work on
// the same instance.
?>
>
Hope somebody helps me - how to define
class and work with one and the same
instance?
>
If this is another page (not included in either of the two previous
files), then $c is no longer available. At the end of the page it gets
destroyed.

If you must work on the same instance, then you will need to store the
object in the $_SESSION in the first page and retrieve in the second.


And P.S. You're English skills are very good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
ZeldorBlat
Guest
 
Posts: n/a
#3: Sep 20 '06

re: Problem with classes



Jag wrote:
Quote:
Hello there!
At the beginning, I'm apologize
for my weak english skills.
I got a problem with the class
which I include from the other
file. It's hard to me to explain
it clearly, so I just give you
files:
>
index.php:
>
<?php
include("someclass.inc.php");
$c=new classA();
$c->SayHallo();
include("another.php");
?>
>
comeclass.inc.php:
>
<?php
class classA()
{
function __construct()
{
echo 'Constructing...';
}
function SayHello()
{
echo "Hello, cruel world.";
}
}
?>
>
another.php:
>
<?php
>
First try:
$c->SayHello(); // error :/
>
Second try:
$c=new classA(); // ALMOST works. It
$c->SayHallo(); // defines second
// instance of the
// class ClassA.
// Unfortunetly, I
// need to work on
// the same instance.
?>
>
Hope somebody helps me - how to define
class and work with one and the same
instance?
>
--
best regards
Jag
If you are, in fact, including another.php inside index.php where you
say you are, then that shouldn't be a problem. What error are you
getting? Could it be that the method of the class is called "SayHello"
and you're trying to call "SayHallo" ?

lorento
Guest
 
Posts: n/a
#4: Sep 21 '06

re: Problem with classes


What version of PHP do you use?

Old version of PHP doesn't support instantiate like this:
$c=new classA();

try to use below instantiate instead of above:
$c = & new classA();

--
http://www.groupvita.com
http://www.deshot.com

Jag wrote:
Quote:
Hello there!
At the beginning, I'm apologize
for my weak english skills.
I got a problem with the class
which I include from the other
file. It's hard to me to explain
it clearly, so I just give you
files:
Jerry Stuckle
Guest
 
Posts: n/a
#5: Sep 21 '06

re: Problem with classes


lorento wrote:
Quote:
Jag wrote:
>
Quote:
>>Hello there!
>>At the beginning, I'm apologize
>>for my weak english skills.
>>I got a problem with the class
>>which I include from the other
>>file. It's hard to me to explain
>>it clearly, so I just give you
>>files:
>
>
What version of PHP do you use?
>
Old version of PHP doesn't support instantiate like this:
$c=new classA();
>
try to use below instantiate instead of above:
$c = & new classA();
>
--
http://www.groupvita.com
http://www.deshot.com
>
(Top posting fixed)

Exactly which version doesn't support it? Every version of PHP I know
of supports

$c = new classA();

P.S. Please don't top post.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jag
Guest
 
Posts: n/a
#6: Sep 21 '06

re: Problem with classes


User "Jerry Stuckle" <jstucklex@attglobal.netwrote:
[...]
Quote:
If you must work on the same instance, then you will
need to store the object in the $_SESSION in the first
page and retrieve in the second.
It was so simple... I feel ashamed that I ask
a question at all! :)
Supposedly, the most simple things are
most difficult. Enough for me.
Thank you very much!
Quote:
And P.S. You're English skills are very good.
It's nice to hear. But I know what I know :)
Thanks again.

--
best regards
Jag


Colin Fine
Guest
 
Posts: n/a
#7: Sep 24 '06

re: Problem with classes


Jerry Stuckle wrote:
Quote:
Jag wrote:
Quote:
>Hello there!
>At the beginning, I'm apologize
>for my weak english skills.
>I got a problem with the class
>which I include from the other
>file. It's hard to me to explain
>it clearly, so I just give you
>files:
>>
>index.php:
>>
><?php
> include("someclass.inc.php");
> $c=new classA();
> $c->SayHallo();
> include("another.php");
>?>
>>
>comeclass.inc.php:
>>
><?php
> class classA()
> {
> function __construct()
> {
> echo 'Constructing...';
> }
> function SayHello()
> {
> echo "Hello, cruel world.";
> }
> }
>?>
>>
>another.php:
>>
><?php
>>
> First try:
> $c->SayHello(); // error :/
>>
> Second try:
> $c=new classA(); // ALMOST works. It
> $c->SayHallo(); // defines second
> // instance of the
> // class ClassA.
> // Unfortunetly, I
> // need to work on
> // the same instance.
>?>
>>
>Hope somebody helps me - how to define
>class and work with one and the same
>instance?
>>
>
If this is another page (not included in either of the two previous
files), then $c is no longer available. At the end of the page it gets
destroyed.
>
It is another page, but it *is* included in index.php, after
$c->SayHallo().

As far as I can see from
http://www.php.net/manual/en/languag...bles.scope.php, $c should be
in scope inside another.php. I don't know why it is failing.
Quote:
If you must work on the same instance, then you will need to store the
object in the $_SESSION in the first page and retrieve in the second.
>
This will presumably work, but I do not see why it should be necessary.
Quote:
>
And P.S. You're English skills are very good.
>
Yes, better than you'res, Jerry! ;-)

Colin
Jerry Stuckle
Guest
 
Posts: n/a
#8: Sep 24 '06

re: Problem with classes


Colin Fine wrote:
Quote:
Jerry Stuckle wrote:
Quote:
Quote:
>>>
>>
>If this is another page (not included in either of the two previous
>files), then $c is no longer available. At the end of the page it
>gets destroyed.
>>
It is another page, but it *is* included in index.php, after
$c->SayHallo().
>
Then it's not another page. It's just a different file included in the
same page right? It should be available. But without all of the code
involved, any guess would be just that - a guess. There are dozens of
possibilities.
Quote:
As far as I can see from
http://www.php.net/manual/en/languag...bles.scope.php, $c should be
in scope inside another.php. I don't know why it is failing.
>
Quote:
>If you must work on the same instance, then you will need to store the
>object in the $_SESSION in the first page and retrieve in the second.
>>
This will presumably work, but I do not see why it should be necessary.
>
Well, if it's all included in the same page then you don't need to do it.
Quote:
Quote:
>>
>And P.S. You're English skills are very good.
>>
Yes, better than you'res, Jerry! ;-)
>
Colin

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