473,395 Members | 1,694 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,395 software developers and data experts.

OO PHP Referencing an instance of a class

Ok all im havign some issues with a class im writing was hoping you all
could help. I dont have access to the actual code here but ill write
out an example of what im trying to do and maybe you all could provide
some pointers.

1: class Foo
2: {
3: $mInstanceBar;
4:
5: function __construct()
6: {
7: $this->mInstanceBar = & new Bar();
8: }
9:
10: function callBarHelloWorld()
11: {
12: $this->mInstanceBar->HelloWorld();
13: }
14: }
15:
16: class Bar
17: {
18: function HelloWorld()
19: {
20: echo "Hello World!";
21: }
22: }
23: $foo = new Foo();
24: $foo->callBarHelloWorld();

Ok line 12 returns an error saying something to the effect that im
calling a function on something that is not an object.

My impression was that on line 7 i am instantiating the object as a
reference which should still be available when called on line 12.

So any ideas?

Apr 11 '06 #1
10 1672
Areric wrote:
Ok all im havign some issues with a class im writing was hoping you all
could help. I dont have access to the actual code here but ill write
out an example of what im trying to do and maybe you all could provide
some pointers.

1: class Foo
2: {
3: $mInstanceBar; private $mInstanceBar; 4:
5: function __construct()
6: {
7: $this->mInstanceBar = & new Bar(); $this->mInstancebar = new Bar(); 8: }
9:
10: function callBarHelloWorld()
11: {
12: $this->mInstanceBar->HelloWorld();
13: }
14: }
15:
16: class Bar
17: {
18: function HelloWorld()
19: {
20: echo "Hello World!";
21: }
22: }
23: $foo = new Foo();
24: $foo->callBarHelloWorld();

Ok line 12 returns an error saying something to the effect that im
calling a function on something that is not an object.

My impression was that on line 7 i am instantiating the object as a
reference which should still be available when called on line 12.

So any ideas?


Apr 11 '06 #2
SO what is the point of the &. I tried that option as well and was told
on another site i should use & to make a reference to the class?

Apr 11 '06 #3
Areric wrote:
Ok all im havign some issues with a class im writing was hoping you all
could help. I dont have access to the actual code here but ill write
out an example of what im trying to do and maybe you all could provide
some pointers.
<snip>
So any ideas?


PHP5 is so nice for this kind of thing...

<?php
class Foo {
private $subObj = NULL;

public function __construct(){
// create an instance of the class we want to use in here
$this->subObj = new Bar();
}

public function __call($method,$args){
// this method is called if we attempt to call a method that is
// not declared in the class definition
if(method_exists($this->subObj,$method)){
return $this->subObj->$method($args);
}else{
echo 'Method does not exist: ',$method;
return FALSE;
}
}

public function fooWorld($string){
echo "<p>Foo: $string</p>\n";
return TRUE;
}
}

class Bar {
public function BarWorld($string){
// since __call will send the method parameters as an array as
// above, we want to compensate for that case as well. May be better
// design to expect an array of parameters to begin with(?)
if(is_array($string)){
$string=join($string);
}
echo "<p>Bar: $string</p>\n";
return TRUE;
}
}

$foo = new Foo();

$foo->fooWorld('Foo string');
$foo->barWorld('Bar string');

/*
OUTPUT:
<p>Foo: Foo string</p>
<p>Bar: Bar string</p>
*/
?>

See:
http://www.php.net/manual/en/languag...verloading.php

HTH
--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Apr 11 '06 #4
Areric wrote:
SO what is the point of the &. I tried that option as well and was told
on another site i should use & to make a reference to the class?

Think of '&' as 'address of'.

So $bar = & new Bar();

is '$bar equals the address of the instance of the class Bar'

To use it, you would have to dereference it twice (once to resolve the
reference and once to resolve the instance).

So if Bar had a method foo(), you would access it as:

$$bar->foo();

As Justin points out, if you really want to use reference to instances
and not just instances, then there is loads of support in PHP5.

-david-

Apr 11 '06 #5
Areric wrote:
5: function __construct()
6: {
7: $this->mInstanceBar = & new Bar();
8: }
9:


The __construct() method is not supported by PHP < 5. In this case, the
constructor should be called Foo().
JW
Apr 11 '06 #6
Janwillem Borleffs wrote:
The __construct() method is not supported by PHP < 5. In this case,
the constructor should be called Foo().


Or, to make your class cross-compatibe, add the following method:

function Foo() {
$this->__construct();
}
JW

Apr 11 '06 #7
Ok that could be it. Im using PHP4 at the moment (shared hosting,
unfortuantly not my choice). Also In response to David above that makes
sense. PHP Takes a very c/c++ approach to classes almost.

Ill try out all of your suggestions and maybe ill set up my spare
machine with php5 and run a 5 version. I really would like to have
access to public / private / protected members as well as interfaces
which from what i understand PHP5 supports.

Im trying to make what im writing very OO-Principle based, including
polymorphism, which implys, to some extent, interfaces.

Apr 11 '06 #8
OK all. Still having issues im going to copy the real code this time
since i have access to it.

class ImageModifierTest //implements ITest
{
var $imageModifier = NULL;
var $testHelpers = NULL;
function __construct()
{
$imageModifier = new ImageModifier();
$testHelpers = new TestHelpers();
}

function ImageModifierTest()
{
$this->__construct();
}

function TestOpenBinaryStream()
{
$streamToSend = "111010001010001";
return $this->imageModifier->OpenBinaryStream($streamToSend);
//THE ABOVE LINE IS WHERE IT ERRORS
return $this->testHelpers->AssertAreEqual($binaryStream,
$streamToSend);
}
function GetBinaryStreamFromFile()
{

$this->imageModifier->SetImageFile('/srv/www/htdocs/images/testpng.png');
$this->imageModifier->CreateBinaryStreamFromFile();
return
$this->testHelpers->AssertIsNotEmpty($this->imageModifier->GetBinaryImageStream);
}
function ExecuteTests()
{
$success = ($this->TestOpenBinaryStream()) &&
($this->GetBinaryStreamFromFile());
if ($success == true)
{
return "Success";
}
else
{
return "Failure";
}
}
}

class ImageModifier
{
//MEMBERS
var $mImage = "";
var $mModifiedImage = "";
var $mBinaryImageStream = "";

function __construct()
{
}

function SetImageFile($image)
{
$this->mImage = $image;
}

function OpenBinaryStream($stream)
{
$this->mImage = $stream;
$this->mBinaryImageStream = $stream;
return $this->mImage;
}

function CreateBinaryStreamFromFile()
{
$fp = fopen($this->mImage, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($this->mImage));
$this->mBinaryImageStream = file_get_contents($this->mImage);
}

function GetBinaryImageStream()
{
return $this->mBinaryImageStream;
}
}
Note i converted my __construct to the method suggested above. I also
got rid of the &s. Im using PHP4 at the moment.

Apr 11 '06 #9
did a little more testing and the __constructor thing didnt matter, it
got into there regardless of what i named it (class name or
__construct)

What i am finding is that my $imageModifier is still NULL when i get
into the test function. I put in a if ($imageModifier == NULL) echo
"ERROR"; and it does indeed print out error.

So any idea why when i KNOW its calling those NEW's in the constructor
it isnt keeping the values once it gets to the function that uses them.

Apr 11 '06 #10
OK all im dumb. i keep forgetting the $this-> stuff in PHP. Im used to
just using the member variable name.

Apr 11 '06 #11

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

Similar topics

11
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
12
by: Mark Broadbent | last post by:
Hi guys, just going through remoting at the moment and a couple of questions relating to .net in general has surfaced. Firstly I have seen in the designer that for the namespace and many of its...
16
by: PromisedOyster | last post by:
Hi I have a situation where I want to use circular referencing. I have cut down the example for demonstration purposes. Say we have one executable (main.exe) and two DLLS (A1.dll and A2.dll)....
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
2
by: Mike Taylor | last post by:
I'm trying to create a base page class for my project (am new to ASP.NET, so am a bit stumped) - I'm using VS.NET 2003, btw. OK, my dummy base page class is shown below: using System; using...
4
by: RSH | last post by:
Okay in my coninuing forms saga I have a situation where I am spawning a new form from my main form: ////////////////////////////////////////////////////////////////////////////////////// ...
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
2
by: HankD | last post by:
Hi, I am having a problem with instantiating two custom objects so they DO NOT point to the same memory location. What is happening is that changes I am making to my object1 are changing object2. I...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
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: 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...
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
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
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...

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.