Jump to content

Trying a class first time user. pretty long way to normal php coding.


Recommended Posts

advance thank you.

 

Am i doing all this OOP correctly, seems to be

a long way to print information to a page.

 

 

My real first attempt.

 


<?php


//set a class.

class userInfo{

// make the class public with variable.

public $name="red";
public $surname="redarrow";
public $age="36 years old";
}

//set a new variable, to output the class names.

$info = new userInfo();

// echo the new class variable of the class.

echo "name:\n". $info->name."<br>";
echo "Surname:\n". $info->surname."<br>";
echo "Age:\n".$info->age."<br>";



?>

I am getting into this ye haaaaaaaaaa !

 

it basic OOP but getting there....

 

<?php

class website{

public $info="This test info is for <br /> the title for any web site!";

}

$header = new website();

echo "<center> <h1> {$header->info} </h1> </center> ";

?>

Why can i not add a function, to the variable $image within the class.

 

get a phase error ((never had them for years lol)).

 

 

 

 

<?php

function doImages(){

$image_array=array("http://t1.gstatic.com/images?q=tbn:kgc1p_6ThobetM:http://www.anthonyshapley.co.uk/wp-content/php_2.jpg","http://images.google.co.uk/imgres?imgurl=http://www.gnunify.in/09/images/php.png&imgrefurl=http://www.gnunify.in/09/content/php-marathon&usg=___8eUuwdxDW7MS8BVapZbXcnlVwg=&h=376&w=576&sz=82&hl=en&start=1&tbnid=6k7fLwXklbmkPM:&tbnh=87&tbnw=134&prev=/images%3Fq%3Dphp%26gbv%3D2%26hl%3Den%26sa%3DG");

foreach($image_array as $images){

echo $images;
}

}


class showImages{

// this is the error code?

public $image=doImages();
}


$img = new  showImages();


echo "{$img->image}";


?>

Can someone cut this down to baby terms now it very hard cheers.......

 

In most circumstances, you will invoke a method using an object variable in conjunction

with -> and the method name. You must use parentheses in your method call as you would if

you were calling a function (even if you are not passing any arguments to the method).

 

<?php
$myObj = new MyClass();

$myObj->myMethod( "Harry", "Palmer" );
?>

 

Let’s declare a method in our ShopProduct class:

 

<?php
class ShopProduct {
public $title = "default product";
public $producerMainName = "main name";
public $producerFirstName = "first name";
public $price = 0;
function getProducer() {
return "{$this->producerFirstName}".
" {$this->producerMainName}";
}
}

$product1 = new ShopProduct();
$product1->title = "My Antonia";
$product1->producerMainName = "Cather";
22 CHAPTER 3 ■ OBJECT BASICS
$product1->producerFirstName = "Willa";
$product1->price = 5.99;
print "author: {$product1->getProducer()}\n";
?>

This outputs the following:

 

author: Willa Cather

<?php

function doImages(){

$image_array=array("http://t1.gstatic.com/images?q=tbn:kgc1p_6ThobetM:http://www.anthonyshapley.co.uk/wp-content/php_2.jpg","http://images.google.co.uk/imgres?imgurl=http://www.gnunify.in/09/images/php.png&imgrefurl=http://www.gnunify.in/09/content/php-marathon&usg=___8eUuwdxDW7MS8BVapZbXcnlVwg=&h=376&w=576&sz=82&hl=en&start=1&tbnid=6k7fLwXklbmkPM:&tbnh=87&tbnw=134&prev=/images%3Fq%3Dphp%26gbv%3D2%26hl%3Den%26sa%3DG");

foreach($image_array as $images){

echo $images;
}

}


class showImages{

// this is the error code?

public $image=doImages();
}


$img = new  showImages();


echo "{$img->image}";


?>

 

well you aren't returning anything with that function, you are echoing things, so thats why you get the error

 

As for the other stuff. Its basically saying that in order to use a member function of a class, you have to use the "->" operator with the object on the left, and the method on the right. The same principle applies when accessing data members also, except you are calling a function.

 

If you have ever used javascript before, you are probably familier with calling methods and data members. for example, the document object has a write method that you should be familiar with.

document.write("Hello World!");

in Javascript, (as well as many other languages) unlike PHP instead of the -> operator, it used the "." operator.

 

Did that answer your question? I'm not quite sure what it was

It doesn't matter if doImages() returned anything. It's not syntactically incorrect. You cannot call functions or methods there. You can only assign constant values in that way.

 

redarrow, the post in your first post here was syntactically correct PHP code, but it was not OOP. OOP is more than just creating a class. Two of the books I recommended you to read yesterday deal with OOP.

Two of the books I recommended you to read yesterday deal with OOP.

 

It looks like he has one of them, as I'm sure he has pasted an extract from "PHP 5 Objects, Patterns and Practice" from Zendstra, above...

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.