Jump to content

Recommended Posts

I've been reading up about Classes and Functions more, to enable me to upgrade some of mine to be .. better.

 

Would this work as a class.

 

 <?php
class myclass
{
      var $myvariable;

      function echothis()
      {
             echo $myvariable;
      }
}

$class = new myclass();
$myvariable = 'this is a string';
$class->echothis();

 

If thats how it works, whats the advantage? When I could simply do..

      function echothis($myvariable)
      {
             echo $myvariable;
      }

$myvariable = 'this is a string';
echothis($myvariable);

 

I'm trying to expand my knowledge. I know this isn't really a major question, just a bit of .. understanding of classes and functions.

Link to comment
https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/
Share on other sites

In your example there would be no benefit. If you don't know the benefits of using OOP you shouldn't be using it because you won't be getting anything out of it anyway. You can easily find a lot of material about OOP so if you're really interested I suggest you read up on it. You can start here.

Your example is a little off anyways....

 

class myclass
{
      public $myvariable;

      function echothis()
      {
             echo $myvariable;
      }
}

$class = new myclass();
$class->myvariable = 'this is a string';
$class->echothis();

 

But AlexWD is right, something so simple won't really demonstrate the advantages of using objects.

Theres alot for me to learn, I'm just trying to pick up on the basics. I'll certainly read what you gave me.

I know in my example, it'd be pretty pointless. Though that's where you start, something.. silly, working your way up.

 

Could you give an explanation of public and private functions?

 

Thorpe, thanks for the correction. That alone helps me understand `public`'s inside classes.

What are `var`'s then? Theres many things for me to actually read upon.

More often than not you can find what you need to know in the manual, and if somewhere else online. http://php.net/manual/en/language.oop5.visibility.php

 

As it states there, public methods and properties are accessible from outside of the object and private one's aren't, basically.

 

class Foo
{
    private $_var = 'value';
    public $var = 'value';

    public function __construct()
    {
        ...
    }
}

$instance = new Foo();
echo $instance->var; // Can access this property since it's public
echo $instance->_var; // Can't access this property because it's private

Yeah.. I understand. Would the same rule apply to private function() and public function() ?

Why doesn't this get any data ? I can echo $class->id and get the correct ID, but it doesn't seem to be putting it into the query?

class profile
{
public $id;

function user_details($profile, $user) {
	$query = mysql_query("select * from `users` where `id`='$id'");
	$row = mysql_fetch_array($query);
                echo $row;
        }
}
$class = new profile();
$class->id = $_GET['uid'];
$class->user_details($profile, $user);

 

I'm just mucking around.. Again, probably another useless technique.

To access properties and methods from within the class you need to use the $this variable.

 

$this->id

 

$query = mysql_query("select * from `users` where `id`='{$this->id}'");
// or
$query = mysql_query("select * from `users` where `id`='" . $this->id . "'");

 

edit: Beat to it  :P

Great stuff. Thorpe, you just didn't put that in the corrected example so.. I was confused.

I better get reading, do you know any good PHP books that are worth buying at all? Might be useful to me.

 

http://www.amazon.co.uk/Programming-PHP-Rasmus-Lerdorf/dp/1565926102

Is this any good?

Building on your example, I can show you one of the benefits of using classes / objects over simple functions. Objects are able to hold state.

 

<?php
class profile
{
    private $_result;

    function getUsers() {
        if ($result = mysql_query("select name from `users` where `id`='$id'")) {
            if (mysql_num_rows($result)) {
                $this->result = $result;
                return true;
            }
        }
        return false;
    }
    
    public function fetch()
    {
        return mysql_fetch_object($this->_result);
    }
}

$profile = new profile();
if ($profile->getUsers()) {
    while ($user = $profile->fetch()) {
        echo $user->name;
    }
}
?>

 

Its still not really a good example of OOP, but it does however highlight the fact that objects can hold state. Notice how the result of your query is stored internally within $_result? This can't be done with simple functions alone.

What do you mean "working on hat" thorpe?

 

<?php
class profile
{
    private $_result;

    function getUsers() {
        if ($result = mysql_query("select name from `users` where `id`='$id'")) {
            if (mysql_num_rows($result)) {
                $this->result = $result;
                return true;
            }
        }
        return false;
    }
    
    public function fetch()
    {
        return mysql_fetch_object($this->_result);
    }
}

$profile = new profile();
if ($profile->getUsers()) {
    while ($user = $profile->fetch()) {
        echo $user->name;
    }
}
?>

 

I like it. So using a method similar to that, how would I create something like..

Where $db is the database connection.

$query = $db->query("my query here");
$row = $query->fetchrow();

Using a method like yours, wouldn't work will it seeing as that one is specifically designed for that one query displayed in getUsers()

 

And would using a method like..

$query = $db->query("my query here");
$row = $query->fetchrow();

be more secure/wise than simply doing..

$query = mysql_query("my query here");
$row = mysql_fetch_array($query);

What do you mean "working on hat" thorpe?

 

I meant 'that'.

 

And would using a method like..

$query = $db->query("my query here");
$row = $query->fetchrow();

be more secure/wise than simply doing..

$query = mysql_query("my query here");
$row = mysql_fetch_array($query);

 

Nope, nothing at all to do with security & if its not done properly, it would be of absolutely no benefit at all.

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.