473,399 Members | 3,603 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,399 software developers and data experts.

What does -> do?

I have these pieces of example code that I am trying to figure out:

(a) $a = $b->c();
(b) $d->e();

What does "->" do?
Where is there a reference/explanation in the PHP manual (or anywhere
else)?

(Unfortunately Google et al. don't search for these characters, so I
don't know how to look for an explanation on this.)

Jan 13 '07 #1
7 1685

Manfred Kooistra wrote:
I have these pieces of example code that I am trying to figure out:

(a) $a = $b->c();
(b) $d->e();

What does "->" do?
Basically, "->" use to reference variables or functions (or more
precisely members and methods) within a class.
eg:
class Something {
public $a = 'Hello';
function __construct($a)
{ $this->a = $a; }
function SetA($a);
{ $this->a = $a; }
function GetA()
{ return $this->a; }
} //end class

Now the member "a" and methods SetA , GetA can be accessed via the "->"
operator
$obj = new Something('World');

//Access method "GetA" of object "obj" using ->
print $obj->GetA();

//accessing member "a" of object "obj" using ->
$obj->a = 'Hello';
print $obj->a;
Where is there a reference/explanation in the PHP manual (or anywhere
else)?

(Unfortunately Google et al. don't search for these characters, so I
don't know how to look for an explanation on this.)
For more information visit:
http://www.php.net/manual/en/language.oop.php
&
http://www.php.net/manual/en/language.oop5.basic.php

Jan 13 '07 #2
Okay, understood: classes and objects
(http://de.php.net/manual/en/language.oop.php).

So why does this work:

require('Class_definition.php');
$object = new Class;
$object->function_defined_in_class($something);
other_function_defined_in_class($object);

but not this:

require('Class_definition.php');
$object = new Class;
while($something) {
$object->function_defined_in_class($something);
}
other_function_defined_in_class($object);

?

Jan 13 '07 #3
Rik
Manfred Kooistra wrote:
Okay, understood: classes and objects
(http://de.php.net/manual/en/language.oop.php).

So why does this work:

require('Class_definition.php');
$object = new Class;
$object->function_defined_in_class($something);
other_function_defined_in_class($object);

but not this:

require('Class_definition.php');
$object = new Class;
while($something) {
$object->function_defined_in_class($something);
}
other_function_defined_in_class($object);
It highly depends on the specific class offcourse. Normally it would work,
unless there's something else going on inside the class. Offcourse, it
would only work when $something is altered by the
function_defined_in_class, else it'd be an infinite loop....
--
Rik Wasmus
Jan 13 '07 #4
Thank you so far.

To make this more concrete:
I am trying to send a variable number of attachments with PEAR::Mail.

With a fixed number of attachments (example with 1 file), the relevant
part of the code was this:

$mime->addAttachment($file, "image/$type_of_file",
$_FILES['upfile_0']['name']);

Now, with an unknown number of files, I changed that part of the code
to (exactly as in my script):

$i = 0;
foreach($_POST_DATA as $key =$value){
if(preg_match("/^upfile_/i", $key)){
$uploaded_file_name = $value;
$uploaded_file_path = $_POST_DATA['upload_dir'] .
$uploaded_file_name;

if(is_file($uploaded_file_path)){
preg_match('/(\.\w+)$/', $uploaded_file_name, $match);
$typ = substr($match[1], 1);

$handle = fopen($uploaded_file_path, 'r');
$file = fread($handle, filesize($uploaded_file_path));
$mime->addAttachment($file, "image/$typ", $uploaded_file_name);
fclose($handle);

$i++;
}
}
}

$_POST_DATA is stuff processed through a Perl script. Files are named
upfile_0, upfile_1 etc. The data is all there, I checked it. I can even
output $file to the screen, but $mime->addAttachment seems not do do
anything, because the emails arrive without attachment (all other
content is okay, since the rest of the [rather very long, so not posted
here] script is unchanged).

It seems to me that somehow the foreach and the -don't work well
together, because I have the same foreach loop for the email content,
where $text .= $sometextfragment; works fine.

Jan 13 '07 #5
Rik
Manfred Kooistra wrote:
Thank you so far.

To make this more concrete:
I am trying to send a variable number of attachments with PEAR::Mail.

With a fixed number of attachments (example with 1 file), the relevant
part of the code was this:

$mime->addAttachment($file, "image/$type_of_file",
$_FILES['upfile_0']['name']);

Now, with an unknown number of files, I changed that part of the code
to (exactly as in my script):
<SNIP>
$_POST_DATA is stuff processed through a Perl script. Files are named
upfile_0, upfile_1 etc. The data is all there, I checked it. I can
even output $file to the screen, but $mime->addAttachment seems not
do do anything, because the emails arrive without attachment (all
other content is okay, since the rest of the [rather very long, so
not posted here] script is unchanged).

It seems to me that somehow the foreach and the -don't work well
together, because I have the same foreach loop for the email content,
where $text .= $sometextfragment; works fine.

There is absolutely no problem in using objects within a loop. It's the
logic that is faulty. I do not use the PEAR::Mail class, but I suspect this
will solve your problem (untested, might be one or two typos):

function return_mime($filename,$fallback){
if(function_exists('finfo_file')){
$finfo = finfo_open(FILEINFO_MIME);
return finfo_file($finfo, $filename);
}
$mime_magic = mime_content_type($filename);
return ($mime_magic==false) ? $fallback : $mime_magic;
}
foreach($_FILES as $file){
if($file['error'] == UPLOAD_ERR_OK){
$mime->addAttachment($file['tmp_name'],
return_mime($file['name'],$file['type']),$file['tmp_name']);
} else {
trigger_error("{$file['name'] is not uploaded correctly.");
}
}

I do not know how your PERL script works, or what it does, so I have not
used that. You should be able te get it to work using this example though.
--
Rik Wasmus
Jan 13 '07 #6
Thank you, Rik.
Alas, that wasn't it.

I found out that the problem was trying to attach the file after
fopening and freading it. On debug, I got an error message telling me:
"File is not readable." I was wondering about that, because that files
permissions and path are alright. So I simply attached it without
fopening it - and that did the trick!

Not working:

$handle = fopen($uploaded_file_path, 'r');
$file = fread($handle, filesize($uploaded_file_path));
$mime->addAttachment($file, "image/$typ", $uploaded_file_name);
fclose($handle);

Working:

$mime->addAttachment($uploaded_file_path, "image/$typ",
$uploaded_file_name);

(with $typ preg_match/substr-ed from the file name)

I wonder about this, because I found hundreds of
mail-attachment-examples with fopen/fread, but I'm happy that my script
is running and I get mailed the uploaded files.

Thank you to all of you!

Jan 13 '07 #7
Rik
Manfred Kooistra wrote:
Thank you, Rik.
Alas, that wasn't it.

I found out that the problem was trying to attach the file after
fopening and freading it. On debug, I got an error message telling me:
"File is not readable." I was wondering about that, because that files
permissions and path are alright. So I simply attached it without
fopening it - and that did the trick!

Not working:

$handle = fopen($uploaded_file_path, 'r');
$file = fread($handle, filesize($uploaded_file_path));
$mime->addAttachment($file, "image/$typ", $uploaded_file_name);
fclose($handle);

Working:

$mime->addAttachment($uploaded_file_path, "image/$typ",
$uploaded_file_name);

(with $typ preg_match/substr-ed from the file name)

I wonder about this, because I found hundreds of
mail-attachment-examples with fopen/fread, but I'm happy that my
script is running and I get mailed the uploaded files.
Well, it always pays to read up on the functions. Does it take the data of
a file, or the filename, that's the only difference.
--
Rik Wasmus
Jan 13 '07 #8

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: Chris Cioffi | last post by:
I started writing this list because I wanted to have definite points to base a comparison on and as the starting point of writing something myself. After looking around, I think it would be a...
65
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
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
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
Oralloy
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,...
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 projectplanning, 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.