Skip to content

Instantly share code, notes, and snippets.

@sveggiani
Last active August 29, 2015 14:04
Show Gist options
  • Save sveggiani/c412e2598e192b20eebf to your computer and use it in GitHub Desktop.
Save sveggiani/c412e2598e192b20eebf to your computer and use it in GitHub Desktop.
Simple class creation and properties asignation #php #class #oop
<?php
class SimpleClass {
public param1;
public param2;
public loggedUser = false;
// constructor, se ejectua al crear una instancia:
// $instancia = new SimpleClass('valueForParam1', 'valueForParam2', $userIdDeOtroLado);
function __construct($param1, $param2, $userId = false) {
$this->param1 = $param1
$this->param2 = $param2
$this->loggedUser = $userId
}
function printUserId() {
echo $this->loggedUser;
}
}
// Puedo crear una instancia seteando todo
$instancia1 = new SimpleClass('one', 'two', $loggedUserId);
// o puedo crear una instancia sin especificar usuario...
$instancia2 = new SimpleClass('three', 'four');
// y asignarlo luego a la propiedad...
$instancia2->loggedUser = $loggedUserId;
// luego hago cosas accediendo a la propiedad
echo $instancia2->loggedUser;
// o usar el método que definimos
$instancia2->printUserId();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment