Postingan lainnya
PHP OOP Inheritance
Mau nanya, saya punya 2 class yaitu parent & child
saya punya method test(); di parent yg panggil properties class nya sendiri pake this, waktu saya warisin ke class child, si method test(); di child tetep panggil properties si parent nya, biar panggil properties nya sendiri gimana ya? Jadi waktu si parent warisin tuh method si child sama parent gapunya hubungan sedarah.
Real codingan nya, saya cuman mau pakai method send dan get message di Client tanpa harus nulis ulang method nya
/** parent
*/{
private string $method = 'aes-128-ctr';
protected string $key;
public function __construct(string $host, int $port)
{
$this->key = openssl_digest(php_uname(), 'SHA256', true);
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($this->socket, $host, $port);
socket_listen($this->socket);
$this->connection = socket_accept($this->socket);
}
final protected function encryptMessage(string $token) : string
{
$ivLength = openssl_cipher_iv_length($this->method);
$iv = openssl_random_pseudo_bytes($ivLength);
$result = openssl_encrypt($token, $this->method, $key, 0, $iv);
return $result . '::' . bin2hex($iv);
}
final protected function decryptMessage(string $token) : string
{
list($token, $this->initialVector) = explode('::', $token);
return openssl_decrypt(
$token,
$this->method,
$this->key,
0,
hex2bin($this->initialVector)
);
}
public function getMessage() : string
{
$encryptedToken = socket_read($this->connection, 1024);
return $this->decryptMessage($encryptedToken);
}
public function sendMessage(string $message) : bool
{
$encrypted = $this->encryptMessage($message);
return socket_write(
$this->connection,
$encrypted,
strlen($encrypted)
);
}
public function __destruct()
{
socket_close($this->connection);
socket_close($this->socket);
}
}
/** child
*/class Client extends Server
{
public function __construct(string $host, int $port)
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($this->socket, $host, $port);
}
public function __destruct()
{
socket_close($this->socket);
}
}
1 Jawaban:
<p>saya kepikirannya jadikan dia parameter</p><pre><br></pre><pre>public function test($item) {<br>$item //ini dari parameter, tergantung yang panggil<br>}<br></pre>
Tanggapan
Contoh kodinganya seperti di atas bang hilman, coba periksa lagi udah saya edit
boleh diedit dulu ngga kodenya? aga susah dibaca
Udah bang, kyaknya ngebug tuh, disini udah ke hilight didalem [code][/code]
ohiya sekarang ngga pake [code][/code] gini lagi, tapi pakai tombolnya di hihgliht dulu baru tekan tombol di pojok kanan. Betul sepertinya ada ngebug sedang diinvestigasi, makasih ya