Nic
Nic

Reputation: 2843

Error using function in a php class

I'm trying to neaten my code by using functions in PHP. I know my function works for sure but when I created a class in PHP to store my function an error appears

Parse error: syntax error, unexpected 'getsquad' (T_STRING), expecting function (T_FUNCTION) in C:\wamp\www\tutorials\squad.php on line 18.

line 18 is where I'm calling my function. for testing proposes.

<?php
include 'simple_html_dom.php';

class squad{

    function getsquad($url){
        $html = file_get_html($url);
        foreach ($html->find('td[align=left]') as $element) {
           if ($element->children(0)) { // work only when children exists
               return $element->children(0)->innertext.'<br>';
           }
        }
    }

    getsquad('$site');

} 

?>

It should be something simple it just that I'm quite new to PHP.

Upvotes: 0

Views: 85

Answers (3)

scrowler
scrowler

Reputation: 24406

I'm going to elaborate a bit on your situation. Let's look at your code:

class squad {
    function getsquad($url){
        $html = file_get_html($url);
        foreach ($html->find('td[align=left]') as $element) {
           if ($element->children(0)) { // work only when children exists
               return $element->children(0)->innertext.'<br>';
           }
        }
    }
    getsquad('$site');
}

So first of all, you can't declare any logic inside classes unless it it inside functions. All you can do outside functions is make declarations for properties that class will use. Let's move past this and assume you've changed it to something like this:

$squad = new squad();
$get_squad = $squad->getsquad();

Now, the logic pattern you are using in your getsquad() function is baaaad. Returning during a loop? Bad bad Nic. From looking at this function, I can assume what you're doing is looping through an HTML file until you find a child, then you return it (end of function). That's cool, but that's not how you should do it - anyone will tell you that you should always return at the end of a function.

function getsquad($url){
    $html = file_get_html($url);
    foreach ($html->find('td[align=left]') as $element) {
       if ($element->children(0)) { // work only when children exists
           return $element->children(0)->innertext.'<br>';
       }
    }
}

So here's what you should do instead:

function getsquad($url){
    $html = file_get_html($url);
    $found_child = null;
    foreach ($html->find('td[align=left]') as $element) {
       if ($element->children(0)) { // work only when children exists
           $found_child = $element->children(0)->innertext . '<br>';
           break; // break ends your foreach loop
       }
    }
    return $found_child;
}

Also, as far as good coding practice is concerned, you shouldn't be formatting any of the data coming from getsquad() - I know this might seem overkill for what you're doing, but this is how you should do it:

function getsquad($url) {
    $html = file_get_html($url);
    $found_child = null;
    foreach ($html->find('td[align=left]') as $element) {
       if ($element->children(0)) { // work only when children exists
           $found_child = $element->children(0)->innertext;
           break; // break ends your foreach loop
       }
    }
    return $this->format_squad($found_child);
}

private function format_squad($found_child) {
    return $found_child . '<br>';
}

Doing this will keep your data and your formatting completely seperate. It's easy to maintain, read and write.

So your final usage (note that you need to assign a variable to store the return of your function [missing from your question and answers]):

$squad = new squad();
// store it in a variable since it's returning data not outputting
$squad_info = $squad->getsquad($url);

Upvotes: 1

Chuck Burgess
Chuck Burgess

Reputation: 11574

You cannot call the method from inside the class. You need to load the class then call the method:

<?php
include 'simple_html_dom.php';

$Squad = new squad();
$Squad->getsquad($site);

class squad{

    function getsquad($url){
        $html = file_get_html($url);
        foreach ($html->find('td[align=left]') as $element) {
           if ($element->children(0)) { // work only when children exists
               return $element->children(0)->innertext.'<br>';
           }
        }
    }
} 
?>

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Can you try this,

 $squad = new squad();
 $squad->getsquad($site);

Your code:

<?php
include 'simple_html_dom.php';

class squad{

function getsquad($url){

$html = file_get_html($url);

  foreach($html->find('td[align=left]') as $element) {
      if ($element->children(0)) { // work only when children exists
          return $element->children(0)->innertext.'<br>';
      }
    }
  }
 } 


// You can use below steps to call the class inside function
$squad = new squad();
$squad->getsquad($site);

?>

Upvotes: 0

Related Questions