• 0

[PHP] Calling functions of a class


Question

I am learning how to call the function of a class in PHP, and I wrote some example for my practice.

<?php

// Class of car
class Car{
		var $type;
		var $modelNo;
		var $seats;
		var $colour;

		function getType(){
				return $type;
		}
		function getModelNo(){
				return $modelNo;
		}
		function getSeats(){
				return $seats;
		}
		function getColour(){
				return $colour;
		}
		function __construct($type, $modelNo, $seats, $colour){
				$this->type = $type;
				$this->modelNo = $modelNo;
				$this->seats = $seats;
				$this->colour = $colour;
		}
}

		// Create an object of type Car
		$myCar = new Car("sports", "A123", 2, "red");
		// Getting information about the car
		echo "Car type: " . $myCar->getType();
?>

Although I have called getType() function, nothing has been returned, thus the result:

Car type:

Did I miss something?

Thanks.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

in your class functions when you use variables its not quite the same as it would be outside of a class.

$variable becomes $this->variable

so try

function getType(){
	return $this->type;
}

Link to comment
Share on other sites

  • 0

1. Make your method public

2. You need to use "$this->" inside of your function as well.

public function getType()
{
  return $this->type; 
}

Link to comment
Share on other sites

  • 0
1. Make your method public

2. You need to use "$this->" inside of your function as well.

public function getType()
{
  return $this->type; 
}

Yeah I forgot about the public

Link to comment
Share on other sites

  • 0
var is outdated, use public, private, protected, etc, for variable declarations.

Not if you are using PHP 4. PHP 5 introduced the visibility concept to the language and supports public, private, and protected declarations for variable and function scope. PHP 4 does not support this.

And, even in PHP 5, you can still use var as a synonym for public on variable declarations, and leave out the public on method declarations (which will be assumed to be public).

Granted, it's not best practice, but it's supported for backwards compatibility.

Link to comment
Share on other sites

  • 0

Thanks everyone... As someone mentioned above, var is outdated. I wonder where to find the most up-to-date tutorials on PHP? Official homepage of PHP?

Thanks.

Link to comment
Share on other sites

  • 0

You could simplify down your example code:

<?php

// Class of car
class Car {
		public $type;
		public $modelNo;
		public $seats;
		public $colour;

		public function __construct($type, $modelNo, $seats, $colour){
				$this->type = $type;
				$this->modelNo = $modelNo;
				$this->seats = $seats;
				$this->colour = $colour;
		}
}


		$car = new Car;
		$car->type = "sports";
		$car->modelNo = "A123";
		$car->seats = 2;
		$car->colour = "red";

		echo "Car type: " . $car->type;
?>

Since all your class attributes are public you can refer to them and set them directly using the $car instance. Notice in my example you don't necessarily need to use your constructor to pass values through to be set in the class either.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.