Classes? - PHP

I’m trying to accomplish my project with classes. I’ve tried a few other things, but I think when it comes down to it, classes would just be best.

Everything I’ve looked up about classes is confusing, uses words and terms I don’t understand. I’m newer to php, so I don’t really get it all yet. I catch on fast, but I’m just stuck with what to do.

So my setup goes something like

<?php
	class Xbox
	{
		public $Variables;
        public $Test;
		
		public function __construct( $variables )
		{
		$this->source = "url here";
		$this->page = @file_get_contents($source);			
		}
        public function __construct( $test )
        {
		$this->tag = str_replace( "%20", " ", $gt );
        }
     }
?>

I’m trying to move most of my code from index.html to box.php. I’m not sure the limitations on some of these variables and stuff.

  1. Am I declaring my variables ($source, $page) correctly?
  2. What does the public$Variables do at the top? Just make it so it can be used throughout the entire class? Or throughout the entire box.php? or what?
  3. Am I using __construct and my functions correctly?
  4. How would I call these functions outside of the class? Or can I?

It seems everything in a class has a different syntax and needs to be done different ways. It’s almost like learning an entirely new language (or maybe its just me?).

Any help would be appreciated, please shed some light!

Your starting to high up the food chain. Learn the basic logic first then once you understand that move onto object-oriented programming. There is nothing wrong with basic procedural programming in the beginning, nothing at all.

See below, I’ve answered your questions in red.

Does that look any better? I declared all used variables as public. I don’t see what is wrong with my __construct’s/methods, could anyone explain it in simpler terms?

It might be better if you start off by showing us a typical source file (keep it succinct, redact any personal details etc ) and also be very specific about the end result you want from that source file after your xbox class has finished doing its stuff.


<?php
    class Xbox
    {
        public $Variables;
        public $Test;
        public $source;
        public $gt;
        public $tag;
        public $page;
        
        public function __construct( $variables, $test )
        {
            if($valiables && $test) {
                $this->source = "url here";
                $this->page = @file_get_contents($this->source);
                $this->tag = str_replace( "%20", " ", $this->gt );
            }
            else {
                throw new Exception('Gimme some bloody data.');
            }       
        }
     }

     try{
         $console = new Xbox(null, null);
     } catch(Exception $e) {
         echo "<span class=\\"error-message\\">Exception caught:" . $e->getMessage(); . "</span>";
     }
?>

That may be what you are looking for.

You don’t need two constructors. You can use logic operations in your constructor. You could then use a Try/Catch block when you create your object. You can handle how you allow or disallow checking of the variables in your controller. I would say you should probably make sure anything you pass directly into a class constructor has relevant functionality of how it is initialized. If not you should probably move some of that out to methods.

Ok, terms…

Method - a function within a class.
Member - a variable within a class.
Element - a method or member of a class is it’s elements.

PHP does not permit multiple methods with the same name in a given class. This is known as function overloading and is possible in other languages (such as C) so long as the functions have different argument types.

The keywords “public”, “private” and “protected” are access controls. The determine what code can access a class element. By access I mean that methods can be called, and members can be read and wrote to.

A public element can be accessed from any point in the code. A protected element can only be accessed by methods within the class or by methods within child classes. A private element can only be accessed within the class itself.

Learning to do things according to OOP structure will not help until you understand the theory behind the structure and why this form of organization is used. If you don’t get a handle on this first you’ll end up writing code that makes you go through all the motions of OOP but doesn’t provide any benefit.

iackay’s example is bad. You should NOT use the __construct method to do anything other than get the object ready to be used. Construct methods are invoked automatically at the time the class is instantiated. Other methods in the class should handle the behaviors.

Here’s an ‘xbox’ example…

Note the use of ‘protected’ property $currentGame, you shouln’t always make properties public.


<?php

class XBox
{
	protected $currentGame;

	// notice only 1 constructor
	public function __construct()
	{
		$this->currentGame = '';
	}
	
	public function insertGame($game)
	{
		$this->currentGame = $game;
	}
	
	public function getCurrentGame()
	{
		return $this->currentGame;
	}
	
	public function playGame()
	{
		if($this->getCurrentGame() == '')
		{
			echo 'no game inserted!';
		}
		else
		{
			echo 'starting game: ' . $this->getCurrentGame();
		}
	}
}

// usage

$console = new XBox();
$console->insertGame('Halo 4');

echo 'the current game is: ' . $console->getCurrentGame() . '<br />';

$console->playGame();

?>

If you want us to teach you, then it might be better if you start off by showing us a typical source file (keep it succinct, redact any personal details etc ) and also be very specific about the end result you want from that source file after your xbox class has finished doing its stuff.

Otherwise just read what has been written and if that makes sense to you, then good for you.

I already have all the code I need in written php, but moving it to the class is what is confusing me.

I’m trying to set up this class, so I can use these variables in multiple different php files, such as index.php as well as images.php. It sounds like my variables are correct to be declared as public, since I will be calling them from multiple different php files.

Does anyone have aim and would be willing to assist me with moving everything over. I’d like to learn how to do it myself, but reading ‘tutorials’ doesn’t seem to be helping me.

I’ve learned a lot about it so far, its just confusing the way everything has to be done in OOP.

I’m still looking for help. Anybody willing?