Forum Moderators: coopster
I'm a newbie about the OOP with php, and I'm triyng to learn this. Well have a problem that I hope someone can help me.
I've created 3 file :
config.php
class_queries.php
index.php
in the config I've create the costant and the class to connect to the db :
<?php
/*
* Connection class for database Mysql
* Define the parameter for the website
* and connect to the database using $connection variable.
*/
define('DB_HOST','localhost');
define('DB_NAME','domain');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('HOST','http://localhost:8888/');
define('DOMAIN','domain.com/');
define('URL',HOST.DOMAIN);
class class_connect {
//put your code here
function __construct(){
}
function connect(){
try{
$conn = mysql_pconnect(DB_HOST,DB_USER,DB_PASSWORD) or die("Errore di connessione");
mysql_select_db(DB_NAME);
echo "connection ok";
}
catch(error $e){
echo 'Error : '.$e->getMessage();
}
}
}
$connection = new class_connect();
$connection->connect();?>
I've used
echo "connection ok";
<?php
include_once('config.php');
class first{function __construct(){
}
function get_last_five(){
echo "the class works";
}
}
?>
My problem is this : if I create the object instance in the class_queries.php it works, but if I create the object in the index.php it doesn't works.
The path is correct, and I've used this code:
$obj = new first;
$obj->get_lsat_five(); I cant understand why doesn't work in the index and works in the same file of the class, where is the error?
Thanks in advantage to all.
:)
My #1 tip (as per last post) is to build objects which basically have a constructor which sets the values - and that's it. An object should be an object, nothing complex, no difficult methods, complex constructors etc. You should wire objects together with wiring (factories, builders) in another file altogether.
Best way to learn - google 'clean code talks' and watch the polymorphism video "The Clean Code Talks -- Inheritance, Polymorphism, & Testing"