Jump to content

Can someone explain how the -> operator in PHP works?


buildakicker

Recommended Posts

The -> accesses the class member (variable or method) of the same name, that is a part of $foobar.  In that example, $foobar has an array variable or string named "header" and that is returning the 0th element.

gizmola did a nice ob of explaining, just to simply give it a name, this is one of the two "object operators" along with ::

Link to comment
Share on other sites

Thanks for the replies! So if nothing is getting returned at $foo, then there is something wrong elsewhere then since $foo has array spot 1 data correct?

 

I am trying to get data from an XML file. Certain tags that is... for example $foo = $foobar->header[0]; where <header> has info in the xml file.

 

<Header>

<Element>

<Description>Select one per Survey</Description>

</Element>

</Header>

 

 

Link to comment
Share on other sites

Thanks for the replies! So if nothing is getting returned at $foo, then there is something wrong elsewhere then since $foo has array spot 1 data correct?

 

It'd be easier if you rephrased your question.

Gizmola did a great job of explaining it but I'm going to assume you're pretty new and explain it step by step.

  • -> isn't an operator it's basically a "selector," used to select a method(function) or variable from a class.
  • So if you try to do echo $foo->bar[0]; and $foo isn't an Object(class) then it won't work.

In order for it to work you need to assign an Object to the $foo variable, for example:

<?php

// it's great practice to always start a class name with an uppercase letter.
class Foo
{
    // public means you can access the variable from within and outside of the class
    public $bar = array();
    
    // __construct is the method that's ran when the class is initiated(when you set it to a variable basically).
    public function __construct ( )
    {
        // $this is referencing THIS CLASS so it's saying "look inside me for a variable called bar and assign it Example." 
        $this->bar[] = 'Example';
    }
}

// assign $f to a new instance of the class Foo
$f = new Foo();

// echo out the first value of the bar array
echo $f->bar[0];

 

I hope you can understand what I mean, if not look up the things you don't understand in the PHP manual it will really help!

Link to comment
Share on other sites

Thanks for the replies! So if nothing is getting returned at $foo, then there is something wrong elsewhere then since $foo has array spot 1 data correct?

 

I am trying to get data from an XML file. Certain tags that is... for example $foo = $foobar->header[0]; where

has info in the xml file.

 

Select one per Survey

 

 

 

Without code, there's no way to look at your problem.  There were some things changed in php5, but it's not possible to comment on whether they might be effecting your code without actually seeing relevant portions.

Link to comment
Share on other sites

do you actually have an issue with your code? my first impression was that this was an informative question only. If you are having an issue with something related to this subject, please post your code and we will be glad to help you

Link to comment
Share on other sites

Yeah, sorry. It's a big chunk of code. I have been trying to get it to work in php5 from a server with 4 on and I am just not sure where this thing is failing. The parser is from: http://www.criticaldevelopment.net/xml/doc.php I switched to the php5 version as an includes, and it still didn't work. So I have been going through the code trying to find what is up...

 

Besides that parser being included, this is the code:

 

// It's probably best to put this file in a non web-accessible folder for security
$configFile = '../documents/WEBRIndex.xml';
$fileContents = file_get_contents($configFile);
$indexXML = new XMLParser($fileContents);
//Work the magic...
$indexXML->Parse();

// ------------------------------------------------------------------
// Associate the users requested view with a document view mapping

$docType = $_GET["doc"];
$action = $_GET["act"];

if(check_not_empty($docType) == FALSE) {
	echo "No document type specified";
	exit;
	}

// Find the XML mapping that corresponds with this type.. the ->document->document reference is because
// there is an embedded ->document attached automatically by the XML Parser... the second ->document
// reference is part of our document

$viewXMLFile = null;
$viewMapping = null;
foreach($indexXML->document->document as $doc) {
    		if(strcmp($doc->documentkey[0]->tagData, $docType) == 0) {
		$viewMapping = $doc;
		$viewXMLFile = $doc->viewxml[0]->tagData;
		break;
		}
	}

// Make sure we found our XML view mapping
if($viewMapping == null) {
	echo "No mapping found";
	exit;
	}

// Now load the actual view XML file into memory
//this file contains the <header><body> etc... 
$viewFile = '../documents/'.$viewXMLFile;
$fileContents = file_get_contents($viewFile);
$viewXML = new XMLParser($fileContents);

//Work the magic...
$viewXML->Parse();

// Find the XML mapping that corresponds with this type (because there can be multiple definitions per view XML file)
// so we need to do this a second time, (the first was for the index file)

$viewMapping = null;
foreach($viewXML->document->documentview as $view) {
    		if(strcmp($view->documentkey[0]->tagData, $docType) == 0) {
		$viewMapping = $view;
		break;
		}
	}

// Make sure we found our XML view mapping
if($viewMapping == null) {
	echo "No mapping found";
	exit;
	}

function check_not_empty($s, $include_whitespace = false)
{
    if ($include_whitespace) {
        // make it so strings containing white space are treated as empty too
        $s = trim($s);
    }
    return (isset($s) && strlen($s)); // var is set and not an empty string ''
}


// ---------------------------------------------------------------
// Extract the header and group data from the view template	

$includeScripts = $viewMapping->includescript;
$customScript = $viewMapping->customscript;
$headerBlock = $viewMapping->header[0];
$bodyBlock = $viewMapping->body[0];
$documentType = $viewMapping->documenttype[0]->tagData;

if(check_not_empty($headerBlock) == FALSE || check_not_empty($bodyBlock) == FALSE || check_not_empty($documentType) == FALSE) {
	echo " Malformed document view - ".check_not_empty($headerBlock)." - ".check_not_empty($bodyBlock)." - ".check_not_empty($documentType);
	exit;
	}

 

Thanks for a look if you have some time :)!

Link to comment
Share on other sites

php error log says:

 

Notice: Undefined index: act in /srv/www/htdocs/webr/app/WEBRView.php on line 40 Notice: Undefined property: XMLTag::$includescript in /srv/www/htdocs/webr/app/WEBRView.php on line 96 Notice: Undefined property: XMLTag::$customscript in /srv/www/htdocs/webr/app/WEBRView.php on line 97 Catchable fatal error: Object of class XMLTag could not be converted to string in /srv/www/htdocs/webr/app/WEBRView.php on line 498

Link to comment
Share on other sites

I commented out line 40, 96 and 97 and got only 1 error at 498.

 

Catchable fatal error: Object of class XMLTag could not be converted to string in /srv/www/htdocs/webr/app/WEBRView.php on line 498

 

Line 498:

function check_not_empty($s, $include_whitespace = false)

{

    if ($include_whitespace) {

        // make it so strings containing white space are treated as empty too

        $s = trim($s);

    }

    return (isset($s) && strlen($s)); // var is set and not an empty string ''

}

 

I think this is an issue with the XMLTag Class... in the http://www.criticaldevelopment.net/xml/parser_php5.phps

script maybe...

 

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.