Jump to content

Recommended Posts

Hello. I am fairly new to the object orientated php. Just wondering if I could get any help with this one. With this code I am attempting to convert a string to upper case.

 


<?php


class Product{
public $type = "Milk";

public function convertToUpperCase(){
strtoupper($type);
}
}

$product = new Product;
echo $product->type;
echo $product->convertToUpperCase($type);


?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/
Share on other sites

Ok im starting to pick things up. I've adjusted the code to

 


<?php
error_reporting(-1);

class Product{
public $type = "Milk";

public function convertToUpperCase(){
$this->type = strtoupper($this->type);
return $this->type;
}
}

$product = new Product;
echo $product->type;
echo $product->convertToUpperCase($type);


?>

 

It now does work, but still produces an error:

 

 

Notice: Undefined variable: type in /home/searchqu/public_html/ww3/main.php on line 15

 

So there is still something not quite right with this line.

echo $product->convertToUpperCase($type);

Link to comment
https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383820
Share on other sites

A bit confused...

 


<form method="POST">
<input type="text" name="someval" />
<input type="submit" name="submit" />
</form>


<?php
error_reporting(-1);

class Product{

public function convertToUpperCase($var){
if(isset($submit))
{
$this->var = strtoupper($this->var);
return $this->var;
}
}
}

$someval = $_POST['someval'];
$submit = $_POST['submit'];
$product = new Product;
echo $product->convertToUpperCase($someval);

?>

 

Once again i get the error of not defining variables:

 

Notice: Undefined index: someval in /home/searchqu/public_html/ww3/main.php on line 22

 

Notice: Undefined index: submit in /home/searchqu/public_html/ww3/main.php on line 23

 

And when i input some text nothing happens.:/

Link to comment
https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383913
Share on other sites

1. You need a class varaible to hold $this->var. Add

public $var;

as the first thing in your class definition.

 

2. You can't use $submit in the class without passing it in. You could potentially check $_POST there since it's a global though.

 

3. Your script structure is all f'd up. :tease-03: But one thing at a time.

Edited by TOA
Link to comment
https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383916
Share on other sites

I didn't test this, but try this

 

<?php
// handle processing and declarations first so you don't get header errors
error_reporting(-1);
class Product{
public $var;
 public function convertToUpperCase($var){
	 if(isset($_POST['submit'])){
		 $this->var = strtoupper($var);
		 return $this->var;
	 }
 }
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// process the form
$someval = $_POST['someval'];
$product = new Product;
echo $product->convertToUpperCase($someval);
} else {
//display the form
<form method="POST">
<input type="text" name="someval" />
<input type="submit" name="submit" />
</form>
}
?>

Edited by TOA
Link to comment
https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383920
Share on other sites

The code you're given on a forum may just be an example, and not designed to be blindly copied and pasted into your application without any problems. If you'd like to blindly copy and paste without reading it and learning how it works, we have a freelancer forum where you can hire employees.

 

'someval' was used in that code to indicate some value, a value you need to provide.

Link to comment
https://forums.phpfreaks.com/topic/269239-php-oo-basic-help/#findComment-1383926
Share on other sites

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.